Back to index

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

List of all members.

Public Member Functions

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

Public Attributes

 default_timeout

Private Member Functions

def _prune
def _get_filename

Private Attributes

 _path
 _threshold

Detailed Description

A cache that stores the items on the file system.  This cache depends
on being the only user of the `cache_dir`.  Make absolutely sure that
nobody but this cache stores files there or otherwise the chace will
randomely delete files therein.

:param cache_dir: the directory where cached files are stored.
: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 432 of file cache.py.


Constructor & Destructor Documentation

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

Definition at line 445 of file cache.py.

00445 
00446     def __init__(self, cache_dir, threshold=500, default_timeout=300):
00447         BaseCache.__init__(self, default_timeout)
00448         self._path = cache_dir
00449         self._threshold = threshold
00450         if not os.path.exists(self._path):
00451             os.makedirs(self._path)


Member Function Documentation

Definition at line 466 of file cache.py.

00466 
00467     def _get_filename(self, key):
00468         hash = md5(key).hexdigest()
00469         return os.path.join(self._path, hash)

Here is the caller graph for this function:

Definition at line 452 of file cache.py.

00452 
00453     def _prune(self):
00454         entries = os.listdir(self._path)
00455         if len(entries) > self._threshold:
00456             now = time()
00457             for idx, key in enumerate(entries):
00458                 try:
00459                     f = file(self._get_filename(key))
00460                     if load(f) > now and idx % 3 != 0:
00461                         f.close()
00462                         continue
00463                 except:
00464                     f.close()
00465                 self.delete(key)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.contrib.cache.FileSystemCache.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 483 of file cache.py.

00483 
00484     def add(self, key, value, timeout=None):
00485         filename = self._get_filename(key)
00486         if not os.path.exists(filename):
00487             self.set(key, value, timeout)

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 from MoinMoin.support.werkzeug.contrib.cache.BaseCache.

Definition at line 509 of file cache.py.

00509 
00510     def clear(self):
00511         for key in os.listdir(self._path):
00512             self.delete(key)

Here is the call graph for this function:

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 503 of file cache.py.

00503 
00504     def delete(self, key):
00505         try:
00506             os.remove(self._get_filename(key))
00507         except (IOError, OSError):
00508             pass

Here is the call graph for this function:

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 470 of file cache.py.

00470 
00471     def get(self, key):
00472         filename = self._get_filename(key)
00473         try:
00474             f = file(filename, 'rb')
00475             try:
00476                 if load(f) >= time():
00477                     return load(f)
00478             finally:
00479                 f.close()
00480             os.remove(filename)
00481         except:
00482             return None

Here is the call graph for this function:

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.FileSystemCache.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 488 of file cache.py.

00488 
00489     def set(self, key, value, timeout=None):
00490         if timeout is None:
00491             timeout = self.default_timeout
00492         filename = self._get_filename(key)
00493         self._prune()
00494         try:
00495             f = file(filename, 'wb')
00496             try:
00497                 dump(int(time() + timeout), f, 1)
00498                 dump(value, f, HIGHEST_PROTOCOL)
00499             finally:
00500                 f.close()
00501         except (IOError, OSError):
00502             pass

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 447 of file cache.py.

Definition at line 448 of file cache.py.

Definition at line 79 of file cache.py.


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