"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/CacheStore.py" (20 Apr 2021, 2966 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "CacheStore.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.0.1_vs_3-3.1.0.

    1 '''
    2 Provides several CacheStore backends for Cheetah's caching framework.  The
    3 methods provided by these classes have the same semantics as those in the
    4 python-memcached API, except for their return values:
    5 
    6 set(key, val, time=0)
    7   set the value unconditionally
    8 add(key, val, time=0)
    9   set only if the server doesn't already have this key
   10 replace(key, val, time=0)
   11   set only if the server already have this key
   12 get(key, val)
   13   returns val or raises a KeyError
   14 delete(key)
   15   deletes or raises a KeyError
   16 '''
   17 import time
   18 
   19 
   20 class Error(Exception):
   21     pass
   22 
   23 
   24 class AbstractCacheStore(object):
   25 
   26     def set(self, key, val, time=None):
   27         raise NotImplementedError
   28 
   29     def add(self, key, val, time=None):
   30         raise NotImplementedError
   31 
   32     def replace(self, key, val, time=None):
   33         raise NotImplementedError
   34 
   35     def delete(self, key):
   36         raise NotImplementedError
   37 
   38     def get(self, key):
   39         raise NotImplementedError
   40 
   41 
   42 class MemoryCacheStore(AbstractCacheStore):
   43     def __init__(self):
   44         self._data = {}
   45 
   46     def set(self, key, val, time=0):
   47         self._data[key] = (val, time)
   48 
   49     def add(self, key, val, time=0):
   50         if key in self._data:
   51             raise Error('a value for key %r is already in the cache' % key)
   52         self._data[key] = (val, time)
   53 
   54     def replace(self, key, val, time=0):
   55         if key in self._data:
   56             raise Error('a value for key %r is already in the cache' % key)
   57         self._data[key] = (val, time)
   58 
   59     def delete(self, key):
   60         del self._data[key]
   61 
   62     def get(self, key):
   63         (val, exptime) = self._data[key]
   64         if exptime and time.time() > exptime:
   65             del self._data[key]
   66             raise KeyError(key)
   67         else:
   68             return val
   69 
   70     def clear(self):
   71         self._data.clear()
   72 
   73 
   74 class MemcachedCacheStore(AbstractCacheStore):
   75     servers = ('127.0.0.1:11211')
   76 
   77     def __init__(self, servers=None, debug=False):
   78         if servers is None:
   79             servers = self.servers
   80         from memcache import Client as MemcachedClient
   81         self._client = MemcachedClient(servers, debug)
   82 
   83     def set(self, key, val, time=0):
   84         self._client.set(key, val, time)
   85 
   86     def add(self, key, val, time=0):
   87         res = self._client.add(key, val, time)
   88         if not res:
   89             raise Error('a value for key %r is already in the cache' % key)
   90         self._data[key] = (val, time)
   91 
   92     def replace(self, key, val, time=0):
   93         res = self._client.replace(key, val, time)
   94         if not res:
   95             raise Error('a value for key %r is already in the cache' % key)
   96         self._data[key] = (val, time)
   97 
   98     def delete(self, key):
   99         res = self._client.delete(key, time=0)
  100         if not res:
  101             raise KeyError(key)
  102 
  103     def get(self, key):
  104         val = self._client.get(key)
  105         if val is None:
  106             raise KeyError(key)
  107         else:
  108             return val
  109 
  110     def clear(self):
  111         self._client.flush_all()