Skip to content

Mapping-like interface #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.*.sw?
*.pyc
*~
.idea/*
dist/*
build/*
*.egg-info/*
.idea/*
.coverage
reg_settings.py
MANIFEST
.directory
19 changes: 18 additions & 1 deletion cachecore/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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::
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 23 additions & 1 deletion test_cachecore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import unittest
import tempfile
import shutil
import collections

from unittest import TestCase
import cachecore as cache
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -165,4 +187,4 @@ def suite():

if __name__ == '__main__':
# print suite()
unittest.main()
unittest.main()