| 1 | ''' |
| 2 | Cache holder classes for Cheetah: |
| 3 | |
| 4 | Cache regions are defined using the #cache Cheetah directive. Each |
| 5 | cache region can be viewed as a dictionary (keyed by cacheRegionID) |
| 6 | handling at least one cache item (the default one). It's possible to add |
| 7 | cacheItems in a region by using the `varyBy` #cache directive parameter as |
| 8 | in the following example:: |
| 9 | |
| 10 | #def getArticle |
| 11 | this is the article content. |
| 12 | #end def |
| 13 | |
| 14 | #cache varyBy=$getArticleID() |
| 15 | $getArticle($getArticleID()) |
| 16 | #end cache |
| 17 | |
| 18 | The code above will generate a CacheRegion and add new cacheItem for each value |
| 19 | of $getArticleID(). |
| 20 | ''' |
| 21 | |
| 22 | try: |
| 23 | from hashlib import md5 |
| 24 | except ImportError: |
| 25 | from md5 import md5 |
| 26 | |
| 27 | import time |
| 28 | from . import CacheStore |
| 29 | |
| 30 | |
| 31 | class CacheItem(object): |
| 32 | ''' |
| 33 | A CacheItem is a container storing: |
| 34 | |
| 35 | - cacheID (string) |
| 36 | - refreshTime (timestamp or None) : last time the cache was refreshed |
| 37 | - data (string) : the content of the cache |
| 38 | ''' |
| 39 | |
| 40 | def __init__(self, cacheItemID, cacheStore): |
| 41 | self._cacheItemID = cacheItemID |
| 42 | self._cacheStore = cacheStore |
| 43 | self._refreshTime = None |
| 44 | self._expiryTime = 0 |
| 45 | |
| 46 | def hasExpired(self): |
| 47 | return (self._expiryTime and time.time() > self._expiryTime) |
| 48 | |
| 49 | def setExpiryTime(self, time): |
| 50 | self._expiryTime = time |
| 51 | |
| 52 | def getExpiryTime(self): |
| 53 | return self._expiryTime |
| 54 | |
| 55 | def setData(self, data): |
| 56 | self._refreshTime = time.time() |
| 57 | self._cacheStore.set(self._cacheItemID, data, self._expiryTime) |
| 58 | |
| 59 | def getRefreshTime(self): |
| 60 | return self._refreshTime |
| 61 | |
| 62 | def getData(self): |
| 63 | assert self._refreshTime |
| 64 | return self._cacheStore.get(self._cacheItemID) |
| 65 | |
| 66 | def renderOutput(self): |
| 67 | """Can be overridden to implement edge-caching""" |
| 68 | return self.getData() or "" |
| 69 | |
| 70 | def clear(self): |
| 71 | self._cacheStore.delete(self._cacheItemID) |
| 72 | self._refreshTime = None |
| 73 | |
| 74 | |
| 75 | class _CacheDataStoreWrapper(object): |
| 76 | def __init__(self, dataStore, keyPrefix): |
| 77 | self._dataStore = dataStore |
| 78 | self._keyPrefix = keyPrefix |
| 79 | |
| 80 | def get(self, key): |
| 81 | return self._dataStore.get(self._keyPrefix + key) |
| 82 | |
| 83 | def delete(self, key): |
| 84 | self._dataStore.delete(self._keyPrefix + key) |
| 85 | |
| 86 | def set(self, key, val, time=0): |
| 87 | self._dataStore.set(self._keyPrefix + key, val, time=time) |
| 88 | |
| 89 | |
| 90 | class CacheRegion(object): |
| 91 | ''' |
| 92 | A `CacheRegion` stores some `CacheItem` instances. |
| 93 | |
| 94 | This implementation stores the data in the memory of the current process. |
| 95 | If you need a more advanced data store, create a cacheStore class that |
| 96 | works with Cheetah's CacheStore protocol and provide it as the cacheStore |
| 97 | argument to __init__. For example you could use |
| 98 | Cheetah.CacheStore.MemcachedCacheStore, a wrapper around the Python |
| 99 | memcached API (http://www.danga.com/memcached). |
| 100 | ''' |
| 101 | _cacheItemClass = CacheItem |
| 102 | |
| 103 | def __init__(self, regionID, templateCacheIdPrefix='', cacheStore=None): |
| 104 | self._isNew = True |
| 105 | self._regionID = regionID |
| 106 | self._templateCacheIdPrefix = templateCacheIdPrefix |
| 107 | if not cacheStore: |
| 108 | cacheStore = CacheStore.MemoryCacheStore() |
| 109 | self._cacheStore = cacheStore |
| 110 | self._wrappedCacheDataStore = _CacheDataStoreWrapper( |
| 111 | cacheStore, keyPrefix=templateCacheIdPrefix + ':' + regionID + ':') |
| 112 | self._cacheItems = {} |
| 113 | |
| 114 | def isNew(self): |
| 115 | return self._isNew |
| 116 | |
| 117 | def clear(self): |
| 118 | " drop all the caches stored in this cache region " |
| 119 | for cacheItemId in list(self._cacheItems.keys()): |
| 120 | cacheItem = self._cacheItems[cacheItemId] |
| 121 | cacheItem.clear() |
| 122 | del self._cacheItems[cacheItemId] |
| 123 | |
| 124 | def getCacheItem(self, cacheItemID): |
| 125 | """ Lazy access to a cacheItem |
| 126 | |
| 127 | Try to find a cache in the stored caches. If it doesn't |
| 128 | exist, it's created. |
| 129 | |
| 130 | Returns a `CacheItem` instance. |
| 131 | """ |
| 132 | cacheItemID = md5(str(cacheItemID).encode('ascii')).hexdigest() |
| 133 | |
| 134 | if cacheItemID not in self._cacheItems: |
| 135 | cacheItem = self._cacheItemClass( |
| 136 | cacheItemID=cacheItemID, |
| 137 | cacheStore=self._wrappedCacheDataStore) |
| 138 | self._cacheItems[cacheItemID] = cacheItem |
| 139 | self._isNew = False |
| 140 | return self._cacheItems[cacheItemID] |