Skip to content

Commit

Permalink
Allow cache debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jan 12, 2015
1 parent 3828df3 commit b808d57
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions walrus/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ class Cache(object):
Cache implementation with simple ``get``/``set`` operations,
and a decorator.
"""
def __init__(self, database, name='cache', default_timeout=None):
def __init__(self, database, name='cache', default_timeout=None,
debug=False):
"""
:param database: :py:class:`Database` instance.
:param name: Namespace for this cache.
:param int default_timeout: Default cache timeout.
:param debug: Disable cache for debugging purposes. Cache will no-op.
"""
self.database = database
self.name = name
self.default_timeout = default_timeout
self.debug = debug

def make_key(self, s):
return ':'.join((self.name, s))
Expand All @@ -27,6 +30,10 @@ def get(self, key, default=None):
does not exist, return the ``default``.
"""
key = self.make_key(key)

if self.debug:
return default

try:
value = self.database[key]
except KeyError:
Expand All @@ -43,6 +50,9 @@ def set(self, key, value, timeout=None):
if timeout is None:
timeout = self.default_timeout

if self.debug:
return True

pickled_value = pickle.dumps(value)
if timeout:
return self.database.setex(key, pickled_value, int(timeout))
Expand All @@ -51,7 +61,8 @@ def set(self, key, value, timeout=None):

def delete(self, key):
"""Remove the given key from the cache."""
self.database.delete(self.make_key(key))
if not self.debug:
self.database.delete(self.make_key(key))

def keys(self):
"""
Expand Down

0 comments on commit b808d57

Please sign in to comment.