diff --git a/src/python/dm/common/utility/objectCache.py b/src/python/dm/common/utility/objectCache.py index 529717b02b810620f5e7b90cad7b813b10da14c4..7a519510b97c862868c07f9b01bde105d4c2895d 100755 --- a/src/python/dm/common/utility/objectCache.py +++ b/src/python/dm/common/utility/objectCache.py @@ -16,13 +16,14 @@ class ObjectCache: # Cache info expiration time. DEFAULT_OBJECT_LIFETIME = 60 # seconds - def __init__(self, cacheSize, objectLifetime=DEFAULT_OBJECT_LIFETIME): + def __init__(self, cacheSize, objectLifetime=DEFAULT_OBJECT_LIFETIME, objectClass=None): self.lock = threading.RLock() self.objectMap = {} # id/object map self.timeStampDeq = deque() # timeStamp deq self.cacheSize = cacheSize self.objectLifetime = objectLifetime self.deqSize = ObjectCache.DEFAULT_TIME_STAMP_DEQ_SIZE_FACTOR*cacheSize + self.objectClass = objectClass def setCacheSize(self, cacheSize): self.cacheSize = cacheSize diff --git a/src/python/dm/common/utility/objectTracker.py b/src/python/dm/common/utility/objectTracker.py new file mode 100755 index 0000000000000000000000000000000000000000..66fd49ccae9b0e49a9d35eb3111d207b4c923093 --- /dev/null +++ b/src/python/dm/common/utility/objectTracker.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import threading +import time + +from dm.common.utility.loggingManager import LoggingManager +from dm.common.utility.configurationManager import ConfigurationManager +from dm.common.utility.objectCache import ObjectCache +from dm.common.utility.singleton import Singleton + +class ObjectTracker(Singleton): + + DEFAULT_CACHE_SIZE = 1000 + DEFAULT_OBJECT_LIFETIME_IN_SECONDS = 86400*30 # 30 days + + # Singleton. + __instanceLock = threading.RLock() + + # Cache configuration + objectClass = None + objectLifetime = DEFAULT_OBJECT_LIFETIME_IN_SECONDS + cacheSize = DEFAULT_CACHE_SIZE + + def __init__(self, *args, **kwargs): + ObjectTracker.__instanceLock.acquire() + try: + self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) + self.logger.debug('Initializing') + self.objectCache = ObjectCache(self.cacheSize, self.objectLifetime, self.objectClass) + self.logger.debug('Initialization complete') + finally: + ObjectTracker.__instanceLock.release() + + def put(self, id, item, objectLifetime=None): + return self.objectCache.put(id, item, objectLifetime) + + def get(self, id): + return self.objectCache.get(id) + + def remove(self, id): + return self.objectCache.remove(id) + +#################################################################### +# Testing + +if __name__ == '__main__': + ot = ObjectTracker.getInstance() + print ot + ot2 = ObjectTracker() + print ot2 + + diff --git a/src/python/dm/daq_web_service/service/impl/experimentTracker.py b/src/python/dm/daq_web_service/service/impl/experimentTracker.py new file mode 100755 index 0000000000000000000000000000000000000000..df84a8f906cca58f1bbb968deef76b5db746ea1e --- /dev/null +++ b/src/python/dm/daq_web_service/service/impl/experimentTracker.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +from dm.common.objects.experiment import Experiment +from dm.common.utility.objectTracker import ObjectTracker + +class ExperimentTracker(ObjectTracker): + + # Cache configuration + objectClass = Experiment + +#################################################################### +# Testing + +if __name__ == '__main__': + et = ExperimentTracker.getInstance() + print et + et2 = ExperimentTracker.getInstance() + print et2 + + +