Skip to content
Snippets Groups Projects
Forked from DM / dm-docs
261 commits behind, 798 commits ahead of the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
objectTracker.py 1.55 KiB
#!/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 getAll(self):
        return self.objectCache.getAll()

    def remove(self, id):
        return self.objectCache.remove(id)

####################################################################
# Testing

if __name__ == '__main__':
    ot = ObjectTracker.getInstance()
    print ot
    ot2 = ObjectTracker()
    print ot2