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

Source Code for Module Pyblosxom.cache.base

  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: base.py 913 2006-08-08 20:29:42Z willhelm $ 
 10  ####################################################################### 
 11  """ 
 12  The cache base class.  Subclasses of this class provide caching for 
 13  blog entry data in PyBlosxom. 
 14  """ 
 15   
 16  __revision__ = "$Revision: 913 $" 
 17   
18 -class BlosxomCacheBase:
19 """ 20 Base Class for Caching stories in pyblosxom. 21 22 A cache is a disposable piece of data that gets updated when an entry 23 is in a fresh state. 24 25 Drivers are to subclass this object, overriding methods defined in 26 this class. If there is an error in creating cache data, be as quite 27 as possible, document how a user could check whether his cache works. 28 29 Driver should expect empty caches and should attempt to create them from 30 scratch. 31 32 @ivar _config: String containing config on where to store the cache. 33 The value of config is derived from C{py['cacheConfig']} in config.py. 34 @type _config: string 35 """
36 - def __init__(self, req, config):
37 """ 38 Constructor - setup and load up the cache 39 40 @param req: the request object 41 @type req: Request 42 43 @param config: String containing config on where to store the cache 44 @type config: string 45 """ 46 self._request = req 47 self._config = config 48 49 self._entryid = "" 50 self._entrydata = {}
51
52 - def load(self, entryid):
53 """ 54 Try to load up the cache with entryid (a unique key for the entry) 55 56 @param entryid: The key identifier for your cache 57 @type entryid: string 58 """ 59 self._entryid = entryid # The filename of the entry 60 self._entrydata = {} # The data of the entry
61
62 - def getEntry(self):
63 """ 64 Gets the data from the cache, returns a dict or an empty dict. 65 """ 66 return self._entrydata
67
68 - def isCached(self):
69 """ 70 Returns 0 or 1 based on whether there is cached data, returns 0 is 71 cache data is stale 72 73 @returns: 0 or 1 based on cache 74 @rtype: boolean 75 """ 76 return 0
77
78 - def saveEntry(self, entrydata):
79 """ 80 Store entrydata in cache 81 82 @param entrydata: The payload, usually a dict 83 @type entrydata: dict 84 """ 85 pass
86
87 - def rmEntry(self):
88 """ 89 Remove cache entry: This is not used by pyblosxom, but used by 90 utilities. 91 """ 92 pass
93
94 - def close(self):
95 """ 96 Override this to close your cache if necessary. 97 """ 98 pass
99
100 - def __getitem__(self, key):
101 """ 102 Convenience function to make this class look like a dict. 103 """ 104 self.load(key) 105 if not self.has_key(key): 106 raise KeyError 107 return self.getEntry()
108
109 - def __setitem__(self, key, value):
110 """ 111 Synonymous to L{saveEntry} 112 """ 113 self.load(key) 114 self.saveEntry(value)
115
116 - def __delitem__(self, key):
117 """ 118 Convenience function to make this look more like a dict. 119 """ 120 self.load(key) 121 self.rmEntry()
122
123 - def has_key(self, key):
124 """ 125 Convenience function to make this look more like a dict. 126 """ 127 self.load(key) 128 return self.isCached()
129
130 - def keys(self):
131 """ 132 List out a list of keys for the cache, to be overridden by a subclass 133 if a full dict interface is required. 134 """ 135 return []
136
137 - def get(self, key, default=None):
138 """ 139 Convenience function to make this look more like a dict. 140 """ 141 try: 142 return self.__getitem__(key) 143 except KeyError: 144 return default
145 146
147 -class BlosxomCache(BlosxomCacheBase):
148 """ 149 A null cache. 150 """ 151 pass
152 153 # vim: tabstop=4 shiftwidth=4 expandtab 154