예제 #1
0
class LRUEngineCache(DataKeyedCache):
    """!
     An instance of DataKeyed cache using a least recently used (LRU) method
    """
    def __init__(self, size=1000):
        """!
        Constructor for LRUCache
        @type size: int
        @param size: maximum entries in the cache
        """

        self.cache = LRUCache(maxsize=size)

    def get_cache_value(self, cache_key):

        return self.cache.get(cache_key)

    def set_cache_value(self, key, value):
        self.cache.__setitem__(key, value)
class ShareUsageTracker(Tracker):
    """!
    The ShareUsageTracker is used by the ShareUsageElement to determine
    whether to put evidence into a bundle to be sent to the 51Degrees
    Share Usage service.
    """
    def __init__(self, size=100, interval=1000):
        """!
        Constructor for ShareUsageTracker
        @type size: int
        @param size: size of the share usage lru cache
        @type interval: int
        @param interval: how often to put evidence into the cache

        """

        self.interval = interval

        self.cache = LRUCache(maxsize=size)

    def match(self, key, value):
        """!
        The track method calls the dataKeyedCache get method,
        if it receives a result it sends it onto a match function
        
        @type key : cache key to run through tracker
        @rtype bool 
        @return result of tracking

        """

        difference = time.time() - value

        if difference > self.interval:
            self.set_cache_value(key, value)
            return True
        else:
            return False

    def get_cache_value(self, cachekey):
        """!
        Get data from the cache
        @type key : string
        @param key : The cache key to lookup
        @type value : mixed
        @param key : None , or the stored data
        """

        return self.cache.get(cachekey)

    def set_cache_value(self, cachekey, value=None):
        """!
        Place data in the cache
        @type key : string
        @param key : The cache key to store data under
        @type value : mixed
        @param key : Not used here as value is set to the date

        """

        self.cache.__setitem__(cachekey, time.time())