Przeglądaj źródła

LocalCache now uses OrderedDict instead of a heap queue

Ask Solem 15 lat temu
rodzic
commit
76fe4b212c
2 zmienionych plików z 8 dodań i 28 usunięć
  1. 8 20
      celery/datastructures.py
  2. 0 8
      celery/tests/test_datastructures.py

+ 8 - 20
celery/datastructures.py

@@ -9,6 +9,8 @@ import traceback
 from UserList import UserList
 from Queue import Queue, Empty as QueueEmpty
 
+from celery.utils.compat import OrderedDict
+
 
 class PositionQueue(UserList):
     """A positional queue of a specific length, with slots that are either
@@ -226,32 +228,18 @@ class LimitedSet(object):
         return self.chronologically[0]
 
 
-class LocalCache(dict):
+class LocalCache(OrderedDict):
     """Dictionary with a finite number of keys.
 
-    Older keys are expired first, but note that the timestamp
-    is not updated if a key is inserted twice, so it's not that great
-    for anything but unique keys.
+    Older items expires first.
 
     """
 
-    def __init__(self, limit=None, initial=None):
+    def __init__(self, limit=None):
         super(LocalCache, self).__init__()
         self.limit = limit
-        self.timestamps = []
-        initial = initial or {}
-        for key, value in initial.items():
-            self[key] = value
 
     def __setitem__(self, key, value):
-        baseproxy = super(LocalCache, self)
-        item = time.time(), key
-        if len(self) >= self.limit:
-            timestamp, expired_key = heapq.heapreplace(self.timestamps, item)
-            baseproxy.pop(expired_key, None)
-        else:
-            heapq.heappush(self.timestamps, item)
-        baseproxy.__setitem__(key, value)
-
-    def __delitem__(self, key):
-        raise NotImplementedError("LocalCache doesn't support deletion.")
+        while len(self) >= self.limit:
+            self.popitem(last=False)
+        super(LocalCache, self).__setitem__(key, value)

+ 0 - 8
celery/tests/test_datastructures.py

@@ -138,11 +138,3 @@ class TestLocalCache(unittest.TestCase):
         for i in slots:
             x[i] = i
         self.assertEquals(x.keys(), slots[limit:])
-        self.assertEquals(len(x.keys()), len(x.timestamps),
-                "timestamps deleted, does not grow.")
-
-    def test_delete_not_implemented(self):
-        self.assertRaises(NotImplementedError, LocalCache(10).__delitem__,
-                "foo")
-
-