Package Pyblosxom :: Package cache :: Module entryshelve
[hide private]
[frames] | no frames]

Source Code for Module Pyblosxom.cache.entryshelve

  1  ####################################################################### 
  2  # This file is part of PyBlosxom. 
  3  # 
  4  # Copyright (c) 2003, 2004, 2005, 2006 Wari Wahab 
  5  #  
  6  # PyBlosxom is distributed under the MIT license.  See the file LICENSE 
  7  # for distribution details. 
  8  # 
  9  # $Id: entryshelve.py 913 2006-08-08 20:29:42Z willhelm $ 
 10  ####################################################################### 
 11  """ 
 12  This cache driver creates shelved data as cache in a dbm file. 
 13   
 14  To use this driver, add the following configuration options in your config.py 
 15   
 16  py['cacheDriver'] = 'entryshelve' 
 17  py['cacheConfig'] = '/path/to/a/cache/dbm/file' 
 18   
 19  If successful, you will see the cache file. Be sure that you have write access 
 20  to the cache file. 
 21  """ 
 22   
 23  __revision__ = "$Revision: 913 $" 
 24   
 25  from Pyblosxom.cache.base import BlosxomCacheBase 
 26  import shelve 
 27  import os 
 28   
29 -class BlosxomCache(BlosxomCacheBase):
30 """ 31 This stores entries in shelves in a .dbm file. 32 """
33 - def __init__(self, req, config):
34 """ 35 Initializes BlosxomCacheBase.__init__ and also opens the 36 shelf file. 37 """ 38 BlosxomCacheBase.__init__(self, req, config) 39 self._db = shelve.open(self._config)
40
41 - def load(self, entryid):
42 """ 43 Loads a specific entryid. 44 """ 45 BlosxomCacheBase.load(self, entryid)
46
47 - def getEntry(self):
48 """ 49 Get an entry from the shelf. 50 """ 51 data = self._db.get(self._entryid, {}) 52 return data.get('entrydata', {})
53
54 - def isCached(self):
55 """ 56 Returns true if the entry is cached and the cached version is 57 not stale. Returns false otherwise. 58 """ 59 data = self._db.get(self._entryid, {'mtime':0}) 60 if os.path.isfile(self._entryid): 61 return data['mtime'] == os.stat(self._entryid)[8] 62 else: 63 return None
64 65
66 - def saveEntry(self, entrydata):
67 """ 68 Save data in the pickled file. 69 """ 70 payload = {} 71 payload['mtime'] = os.stat(self._entryid)[8] 72 payload['entrydata'] = entrydata 73 74 self._db[self._entryid] = payload
75 76
77 - def rmEntry(self):
78 """ 79 Removes an entry from the shelf. 80 """ 81 if self._db.has_key(self._entryid): 82 del self._db[self._entryid]
83
84 - def keys(self):
85 """ 86 Returns a list of entries that are cached in the shelf. 87 88 @returns: list of entry paths 89 @rtype: list of strings 90 """ 91 ret = [] 92 for key in self._db.keys(): 93 self.load(key) 94 if self.isCached(): 95 ret.append(key) 96 else: 97 # Remove this key, why is it there in the first place? 98 del self._db[self._entryid] 99 return ret
100 101
102 - def close(self):
103 """ 104 Closes the db file. 105 """ 106 self._db.close() 107 self._db = None
108 109 # vim: tabstop=4 shiftwidth=4 expandtab 110