#!/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