Mike Bayer avatar Mike Bayer committed d24c2d6

add the memory backend

Comments (0)

Files changed (3)

dogpile/cache/backends/__init__.py

 
 register_backend("dbm", "dogpile.cache.backends.dbm", "DBMBackend")
 register_backend("pylibmc", "dogpile.cache.backends.memcached", "PylibmcBackend")
+register_backend("memory", "dogpile.cache.backends.memory", "MemoryBackend")

dogpile/cache/backends/memory.py

+from dogpile.cache.api import CacheBackend, CachedValue, NO_VALUE
+
+class MemoryBackend(CacheBackend):
+    def __init__(self, the_cache=None):
+        if the_cache is None:
+            self._cache = {}
+        else:
+            self._cache = the_cache
+
+    def get(self, key):
+        return self._cache.get(key, NO_VALUE)
+
+    def set(self, key, value):
+        self._cache[key] = value
+
+    def delete(self, key):
+        self._cache.pop(key, None)
+

tests/test_memory_backend.py

+from unittest import TestCase
+from dogpile.cache.api import CacheBackend, CachedValue, NO_VALUE
+from dogpile.cache import register_backend, CacheRegion
+from tests import eq_, assert_raises_message
+import time
+import itertools
+
+class MemoryBackendTest(TestCase):
+
+    def _region(self, init_args={}, config_args={}, backend="memory"):
+        reg = CacheRegion(**init_args)
+        reg.configure(backend, **config_args)
+        return reg
+
+    def test_set_get_value(self):
+        reg = self._region()
+        reg.set("some key", "some value")
+        eq_(reg.get("some key"), "some value")
+
+    def test_set_get_nothing(self):
+        reg = self._region()
+        eq_(reg.get("some key"), NO_VALUE)
+
+    def test_creator(self):
+        reg = self._region()
+        def creator():
+            return "some value"
+        eq_(reg.get_or_create("some key", creator), "some value")
+
+    def test_remove(self):
+        reg = self._region()
+        reg.set("some key", "some value")
+        reg.delete("some key")
+        reg.delete("some key")
+        eq_(reg.get("some key"), NO_VALUE)
+
+    def test_expire(self):
+        reg = self._region(config_args={"expiration_time":1})
+        counter = itertools.count(1)
+        def creator():
+            return "some value %d" % next(counter)
+        eq_(reg.get_or_create("some key", creator), "some value 1")
+        time.sleep(1)
+        eq_(reg.get("some key"), "some value 1")
+        eq_(reg.get_or_create("some key", creator), "some value 2")
+        eq_(reg.get("some key"), "some value 2")
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.