diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f18108 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.*.sw? +*.pyc +*~ +.idea/* +dist/* +build/* +*.egg-info/* +.idea/* +.coverage +reg_settings.py +MANIFEST +.directory diff --git a/cachecore/core.py b/cachecore/core.py index 39e38d3..e8b0ee9 100644 --- a/cachecore/core.py +++ b/cachecore/core.py @@ -59,6 +59,7 @@ def get_sidebar(user): import os import re import tempfile +from collections import MutableMapping try: from hashlib import md5 except ImportError: @@ -89,7 +90,7 @@ def _items(mappingorseq): else mappingorseq -class BaseCache(object): +class BaseCache(MutableMapping): """Baseclass for the cache systems. All the cache systems implement this API or a superset of it. @@ -98,8 +99,15 @@ class BaseCache(object): """ def __init__(self, default_timeout=300): + super(BaseCache, self).__init__() self.default_timeout = default_timeout + def __iter__(self): + raise StopIteration + + def __len__(self): + raise NotImplementedError + def get(self, key): """Looks up key in the cache and returns the value for it. If the key does not exist `None` is returned instead. @@ -108,6 +116,9 @@ def get(self, key): """ return None + def __getitem__(self, key): + return self.get(key) + def delete(self, key): """Deletes `key` from the cache. If it does not exist in the cache nothing happens. @@ -116,6 +127,9 @@ def delete(self, key): """ pass + def __delitem__(self, key): + return self.delete(key) + def get_many(self, *keys): """Returns a list of values for the given keys. For each key a item in the list is created. Example:: @@ -153,6 +167,9 @@ def set(self, key, value, timeout=None): """ pass + def __setitem__(self, key, value): + return self.set(key, value) + def add(self, key, value, timeout=None): """Works like :meth:`set` but does not overwrite the values of already existing keys. diff --git a/test_cachecore.py b/test_cachecore.py index 830bcd0..aa9563d 100755 --- a/test_cachecore.py +++ b/test_cachecore.py @@ -6,6 +6,7 @@ import unittest import tempfile import shutil +import collections from unittest import TestCase import cachecore as cache @@ -78,6 +79,27 @@ def test_filesystemcache_clear(self): shutil.rmtree(tmp_dir) +class MappingTestCase(TestCase): + def test_should_have_MutableMapping_methods_and_inherit_from_it(self): + self.assertIn(collections.MutableMapping, cache.BaseCache.mro()) + my_cache = cache.BaseCache() + expected_methods = ['setitem', 'getitem', 'delitem', 'iter', 'len'] + actual_methods = dir(my_cache) + for method in expected_methods: + self.assertIn('__{}__'.format(method), actual_methods) + + def test_should_be_able_to_use_it_as_a_dict(self): + my_cache = cache.SimpleCache() + my_cache['python'] = 'rules' + my_cache.set('answer', '42') + self.assertEquals(my_cache.get('python'), 'rules') + self.assertEquals(my_cache['answer'], '42') + del my_cache['python'] + my_cache.delete('answer') + self.assertEquals(my_cache['python'], None) + self.assertEquals(my_cache['answer'], None) + + # class RedisCacheTestCase(TestCase): # def make_cache(self): @@ -165,4 +187,4 @@ def suite(): if __name__ == '__main__': # print suite() - unittest.main() \ No newline at end of file + unittest.main()