Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
MoinMoin.support.werkzeug.contrib.cache.SimpleCache Class Reference
Inheritance diagram for MoinMoin.support.werkzeug.contrib.cache.SimpleCache:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.werkzeug.contrib.cache.SimpleCache:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def get
def set
def add
def delete
def get_many
def get_dict
def set_many
def delete_many
def clear
def inc
def dec

Public Attributes

 clear
 default_timeout

Private Member Functions

def _prune

Private Attributes

 _cache
 _threshold

Detailed Description

Simple memory cache for single process environments.  This class exists
mainly for the development server and is not 100% thread safe.  It tries
to use as many atomic operations as possible and no locks for simplicity
but it could happen under heavy load that keys are added multiple times.

:param threshold: the maximum number of items the cache stores before
                  it starts deleting some.
:param default_timeout: the default timeout that is used if no timeout is
                        specified on :meth:`~BaseCache.set`.

Definition at line 200 of file cache.py.


Constructor & Destructor Documentation

def MoinMoin.support.werkzeug.contrib.cache.SimpleCache.__init__ (   self,
  threshold = 500,
  default_timeout = 300 
)

Definition at line 212 of file cache.py.

00212 
00213     def __init__(self, threshold=500, default_timeout=300):
00214         BaseCache.__init__(self, default_timeout)
00215         self._cache = {}
00216         self.clear = self._cache.clear
00217         self._threshold = threshold


Member Function Documentation

Definition at line 218 of file cache.py.

00218 
00219     def _prune(self):
00220         if len(self._cache) > self._threshold:
00221             now = time()
00222             for idx, (key, (expires, _)) in enumerate(self._cache.items()):
00223                 if expires <= now or idx % 3 == 0:
00224                     self._cache.pop(key, None)

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.SimpleCache.add (   self,
  key,
  value,
  timeout = None 
)
Works like :meth:`set` but does not override already existing
values.

:param key: the key to set
:param value: the value for the key
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 237 of file cache.py.

00237 
00238     def add(self, key, value, timeout=None):
00239         if timeout is None:
00240             timeout = self.default_timeout
00241         if len(self._cache) > self._threshold:
00242             self._prune()
00243         item = (time() + timeout, dumps(value, HIGHEST_PROTOCOL))
00244         self._cache.setdefault(key, item)

Here is the call graph for this function:

Here is the caller graph for this function:

Clears the cache.  Keep in mind that not all caches support
clearning of the full cache.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.FileSystemCache, and MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 163 of file cache.py.

00163 
00164     def clear(self):
00165         """Clears the cache.  Keep in mind that not all caches support
00166         clearning of the full cache.
00167         """
00168         pass

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.dec (   self,
  key,
  delta = 1 
) [inherited]
Decrements the value of a key by `delta`.  If the key does
not yet exist it is initialized with `-delta`.

For supporting caches this is an atomic operation.

:param key: the key to increment.
:param delta: the delta to subtract.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 180 of file cache.py.

00180 
00181     def dec(self, key, delta=1):
00182         """Decrements the value of a key by `delta`.  If the key does
00183         not yet exist it is initialized with `-delta`.
00184 
00185         For supporting caches this is an atomic operation.
00186 
00187         :param key: the key to increment.
00188         :param delta: the delta to subtract.
00189         """
00190         self.set(key, (self.get(key) or 0) - delta)
00191 

Here is the call graph for this function:

Deletes `key` from the cache.  If it does not exist in the cache
nothing happens.

:param key: the key to delete.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 245 of file cache.py.

00245 
00246     def delete(self, key):
00247         self._cache.pop(key, None)
00248 

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.delete_many (   self,
  keys 
) [inherited]
Deletes multiple keys at once.

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 154 of file cache.py.

00154 
00155     def delete_many(self, *keys):
00156         """Deletes multiple keys at once.
00157 
00158         :param keys: The function accepts multiple keys as positional
00159                      arguments.
00160         """
00161         for key in keys:
00162             self.delete(key)

Here is the call graph for this function:

Looks up key in the cache and returns it.  If the key does not
exist `None` is returned instead.

:param key: the key to be looked up.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 225 of file cache.py.

00225 
00226     def get(self, key):
00227         now = time()
00228         expires, value = self._cache.get(key, (0, None))
00229         if expires > time():
00230             return loads(value)

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.get_dict (   self,
  keys 
) [inherited]
Works like :meth:`get_many` but returns a dict::

    d = cache.get_dict("foo", "bar")
    foo = d["foo"]
    bar = d["bar"]

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 111 of file cache.py.

00111 
00112     def get_dict(self, *keys):
00113         """Works like :meth:`get_many` but returns a dict::
00114 
00115             d = cache.get_dict("foo", "bar")
00116             foo = d["foo"]
00117             bar = d["bar"]
00118 
00119         :param keys: The function accepts multiple keys as positional
00120                      arguments.
00121         """
00122         return dict(izip(keys, self.get_many(*keys)))

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.get_many (   self,
  keys 
) [inherited]
Returns a list of keys.  For each key a item in the list is
created.  Example::

    foo, bar = cache.get_many("foo", "bar")

If a key can't be looked up `None` is returned for that key
instead.

:param keys: The function accepts multiple keys as positional
     arguments.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 97 of file cache.py.

00097 
00098     def get_many(self, *keys):
00099         """Returns a list of keys.  For each key a item in the list is
00100         created.  Example::
00101 
00102             foo, bar = cache.get_many("foo", "bar")
00103 
00104         If a key can't be looked up `None` is returned for that key
00105         instead.
00106 
00107         :param keys: The function accepts multiple keys as positional
00108                      arguments.
00109         """
00110         return map(self.get, keys)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.inc (   self,
  key,
  delta = 1 
) [inherited]
Increments the value of a key by `delta`.  If the key does
not yet exist it is initialized with `delta`.

For supporting caches this is an atomic operation.

:param key: the key to increment.
:param delta: the delta to add.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 169 of file cache.py.

00169 
00170     def inc(self, key, delta=1):
00171         """Increments the value of a key by `delta`.  If the key does
00172         not yet exist it is initialized with `delta`.
00173 
00174         For supporting caches this is an atomic operation.
00175 
00176         :param key: the key to increment.
00177         :param delta: the delta to add.
00178         """
00179         self.set(key, (self.get(key) or 0) + delta)

Here is the call graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.SimpleCache.set (   self,
  key,
  value,
  timeout = None 
)
Adds or overrides a key in the cache.

:param key: the key to set
:param value: the value for the key
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 231 of file cache.py.

00231 
00232     def set(self, key, value, timeout=None):
00233         if timeout is None:
00234             timeout = self.default_timeout
00235         self._prune()
00236         self._cache[key] = (time() + timeout, dumps(value, HIGHEST_PROTOCOL))

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.BaseCache.set_many (   self,
  mapping,
  timeout = None 
) [inherited]
Sets multiple keys and values from a dict.

:param mapping: a dict with the values to set.
:param timeout: the cache timeout for the key or the default
        timeout if not specified.

Reimplemented in MoinMoin.support.werkzeug.contrib.cache.MemcachedCache.

Definition at line 144 of file cache.py.

00144 
00145     def set_many(self, mapping, timeout=None):
00146         """Sets multiple keys and values from a dict.
00147 
00148         :param mapping: a dict with the values to set.
00149         :param timeout: the cache timeout for the key or the default
00150                         timeout if not specified.
00151         """
00152         for key, value in mapping.iteritems():
00153             self.set(key, value, timeout)

Here is the call graph for this function:


Member Data Documentation

Definition at line 214 of file cache.py.

Definition at line 216 of file cache.py.

Definition at line 215 of file cache.py.

Definition at line 79 of file cache.py.


The documentation for this class was generated from the following file: