Example #1
0
    def cached(self, func, *args, **kwargs):
        """
        >>> r = RamLRUCache({'size' : 5})
        >>> r._cache =  {'a': {'([1, 2], {})': {'{}': 3}}}
        >>> r.cached('a',[1, 2], {})
        3
        >>> r._order
        ['a||(([1, 2], {}), {})||{}']
        >>> r._cache = {'a': {'([1, 2], {})': {'{}': 3}}, 'c': {'([1, 3], {})': {'{}': 3.1400000000000001}}, 'b': {'([1, 3], {})': {'{}': 66}}}
        >>> r._order = ['a||(([1, 2], {}), {})||{}', 'b||(([1, 3], {}), {})||{}', 'c||(([1, 3], {}), {})||{}']
        >>> r._order
        ['a||(([1, 2], {}), {})||{}', 'b||(([1, 3], {}), {})||{}', 'c||(([1, 3], {}), {})||{}']
        >>> r.cached('a',[1, 2], {})
        3
        >>> r._order
        ['b||(([1, 3], {}), {})||{}', 'c||(([1, 3], {}), {})||{}', 'a||(([1, 2], {}), {})||{}']
        """
        
        key = self._computeKey(func, args, kwargs )

        if key in self._order:
            self._order.remove(key)
           
        self._order.append(key)
        
        return RamCache.cached(self, func, *args, **kwargs)
Example #2
0
    def putInCache(self, func, result, *args, **kwargs):
        """
        >>> r = RamLRUCache({'size' : 5})
        >>> r.putInCache('a', 3, [1, 2], {})
        3
        >>> r._order
        ['a||(([1, 2], {}), {})||{}']
        >>> r.putInCache('b', 66, [1, 3], {})
        66
        >>> r.putInCache('c', 3.14, [1, 3], {})
        3.1400000000000001
        >>> r._order
        ['a||(([1, 2], {}), {})||{}', 'b||(([1, 3], {}), {})||{}', 'c||(([1, 3], {}), {})||{}']
        >>> r._cache
        {'a': {'([1, 2], {})': {'{}': 3}}, 'c': {'([1, 3], {})': {'{}': 3.1400000000000001}}, 'b': {'([1, 3], {})': {'{}': 66}}}
        """

        key = self._computeKey(func, args, kwargs )

        self._order.append(key)
        if len(self._order) >= self._size:
            tmp = self._order.pop(0).split('||')
            del(_cache[tmp[0]][tmp[1]][tmp[2]])

        return RamCache.putInCache(self, func, result, *args, **kwargs)
    def __init__(self, config):
        """
        Ram Cache.

        >>> r = TimeLimitedRamCache({'duration' : 2}) 
        >>> r #doctest: +ELLIPSIS
        <__main__.TimeLimitedRamCache object at 0x...>
        >>> r._cache
        {}
        >>> r._dateHit
        {}
        >>> r._timedelta
        datetime.timedelta(0, 2)
        """
        RamCache.__init__(self)

        self._timedelta = datetime.timedelta(seconds=config['duration'])
        self._dateHit = {}
Example #4
0
    def __init__(self, config=None):
        """
        Ram Cache.

        >>> r = RamLRUCache({'size' : 5}) 
        >>> r #doctest: +ELLIPSIS
        <__main__.RamLRUCache object at 0x...>
        >>> r._cache
        {}
        >>> r._order
        []
        >>> r._size
        5
        """
        
        RamCache.__init__(self)

        self._order = []
        self._size = int(config['size'])