add 'cache' property

This commit is contained in:
2013-09-07 17:25:22 +02:00
parent f8b0a53c3f
commit 77c1ccf40b
6 changed files with 187 additions and 121 deletions

View File

@ -27,34 +27,33 @@ class Cache(object):
self._cache = {}
self.storage = storage
def setcache(self, cache_type, path, val, time):
def setcache(self, path, val, time):
self._cache[path] = (val, time)
def getcache(self, cache_type, path, exp):
def getcache(self, path, exp):
value, created = self._cache[path]
if exp < created:
if exp <= created:
return True, value
return False, None
def hascache(self, cache_type, path):
def hascache(self, path):
""" path is in the cache
:param cache_type: value | property
:param path: the path's option
"""
return path in self._cache
def reset_expired_cache(self, cache_type, exp):
def reset_expired_cache(self, exp):
for key in tuple(self._cache.keys()):
val, created = self._cache[key]
if exp > created:
if created is not None and exp > created:
del(self._cache[key])
def reset_all_cache(self, cache_type):
def reset_all_cache(self):
"empty the cache"
self._cache.clear()
def get_cached(self, cache_type, context):
def get_cached(self, context):
"""return all values in a dictionary
example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
"""