Skip to content
Snippets Groups Projects
Commit a5ee031e authored by sveseli's avatar sveseli
Browse files

added generic tracker class that uses object cache; added experiment tracker to daq web service

parent 419a20fa
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
#!/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
#!/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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment