Exemplo n.º 1
0
 def test_clear(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(len(od), len(pairs))
     od.clear()
     self.assertEqual(len(od), 0)
Exemplo n.º 2
0
 def test_clear(self):
     pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
     shuffle(pairs)
     od = OrderedDict(pairs)
     self.assertEqual(len(od), len(pairs))
     od.clear()
     self.assertEqual(len(od), 0)
Exemplo n.º 3
0
class DictCache(AbstractCache):
    """Implementation of a cache in a memory dictionary

    """
    def __init__(self, ttl=60):
        """Creates a new instance

        params:

            ``ttl``
                Time to live of the data.

        """
        super(DictCache, self).__init__(ttl=ttl)
        try:
            self._ich = collections.OrderedDict()
            self._ttds = collections.OrderedDict()
        except AttributeError:
            #This version of python does not support OrderedDict
            from ordereddict import OrderedDict
            self._ich = OrderedDict()
            self._ttds = OrderedDict()

    def store_data(self, k, ttl, v):
        self._ich[k] = v
        self._ttds[k] = (
            time.time() + ttl if ttl != None else None
        )

    def retrieve_data(self, k):
        ttd = self._ttds.get(k, 0)
        if ttd == None or time.time() < ttd:
            return self._ich[k]
        elif ttd:
            self._ttds.pop(k)
            self._ich.pop(k)

    def clear_expired(self):
        for k, ttd in self._ttds.items():
            if ttd != None and ttd < time.time():
                self._ttds.pop(k)
                self._ich.pop(k)
            else:
                break

    def clear(self):
        self._ich.clear()
        self._ttds.clear()