"""
import os
-import base64
import codecs
from hashlib import md5
class BaseCache:
def get(self, key):
- raise NotImplemented()
+ """Get cache value"""
+ raise NotImplementedError
def set(self, key, value):
- raise NotImplemented()
+ """Set cache value"""
+ raise NotImplementedError
def delete(self, key):
- raise NotImplemented()
+ """Remove cache value"""
+ raise NotImplementedError
class DictCache(BaseCache):
self.forever = forever
if not os.path.isdir(self.directory):
- os.mkdir(self.directory)
+ os.makedirs(self.directory)
- def encode(self, x):
- return md5(x.encode('utf-8')).hexdigest()
+ def encode(self, val):
+ return md5(val.encode('utf-8')).hexdigest()
def _fn(self, name):
return os.path.join(self.directory, self.encode(name))
def set(self, key, value):
name = self._fn(key)
with FileLock(name):
- with codecs.open(name, 'w+b') as fh:
- dump(value, fh)
+ with codecs.open(name, 'w+b') as flh:
+ dump(value, flh)
def delete(self, key):
if not self.forever:
os.remove(self._fn(key))
+
+ def __iter__(self):
+ for dirpath, _, filenames in os.walk(self.directory):
+ for item in filenames:
+ name = os.path.join(dirpath, item)
+ yield load(codecs.open(name, 'rb'))