comparison piecrust/cache.py @ 687:61d606fbc313

bake: Change `show-timers` to `show-stats`, add stats.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 14 Mar 2016 08:26:14 -0700
parents 5feb71d31a4f
children a066f4ac9094
comparison
equal deleted inserted replaced
686:1a6c4c2683fd 687:61d606fbc313
160 def __init__(self, size=2048): 160 def __init__(self, size=2048):
161 self.cache = repoze.lru.LRUCache(size) 161 self.cache = repoze.lru.LRUCache(size)
162 self.fs_cache = None 162 self.fs_cache = None
163 self._last_access_hit = None 163 self._last_access_hit = None
164 self._invalidated_fs_items = set() 164 self._invalidated_fs_items = set()
165 self._missed_keys = []
166 self._misses = 0
167 self._hits = 0
165 168
166 @property 169 @property
167 def last_access_hit(self): 170 def last_access_hit(self):
168 return self._last_access_hit 171 return self._last_access_hit
169 172
183 self.fs_cache.write(fs_key, item_raw) 186 self.fs_cache.write(fs_key, item_raw)
184 187
185 def get(self, key, item_maker, fs_cache_time=None, save_to_fs=True): 188 def get(self, key, item_maker, fs_cache_time=None, save_to_fs=True):
186 self._last_access_hit = True 189 self._last_access_hit = True
187 item = self.cache.get(key) 190 item = self.cache.get(key)
188 if item is None: 191 if item is not None:
189 if (self.fs_cache is not None and 192 self._hits += 1
190 fs_cache_time is not None): 193 return item
191 # Try first from the file-system cache. 194
192 fs_key = _make_fs_cache_key(key) 195 if (self.fs_cache is not None and
193 if (fs_key not in self._invalidated_fs_items and 196 fs_cache_time is not None):
194 self.fs_cache.isValid(fs_key, fs_cache_time)): 197 # Try first from the file-system cache.
195 logger.debug("'%s' found in file-system cache." % 198 fs_key = _make_fs_cache_key(key)
196 key) 199 if (fs_key not in self._invalidated_fs_items and
197 item_raw = self.fs_cache.read(fs_key) 200 self.fs_cache.isValid(fs_key, fs_cache_time)):
198 item = json.loads( 201 logger.debug("'%s' found in file-system cache." %
199 item_raw, 202 key)
200 object_pairs_hook=collections.OrderedDict) 203 item_raw = self.fs_cache.read(fs_key)
201 self.cache.put(key, item) 204 item = json.loads(
202 return item 205 item_raw,
203 206 object_pairs_hook=collections.OrderedDict)
204 # Look into the mem-cache. 207 self.cache.put(key, item)
205 logger.debug("'%s' not found in cache, must build." % key) 208 self._hits += 1
206 item = item_maker() 209 return item
207 self.cache.put(key, item) 210
208 self._last_access_hit = False 211 # Look into the mem-cache.
209 212 logger.debug("'%s' not found in cache, must build." % key)
210 # Save to the file-system if needed. 213 item = item_maker()
211 if self.fs_cache is not None and save_to_fs: 214 self.cache.put(key, item)
212 item_raw = json.dumps(item) 215 self._last_access_hit = False
213 self.fs_cache.write(fs_key, item_raw) 216 self._misses += 1
217 self._missed_keys.append(key)
218
219 # Save to the file-system if needed.
220 if self.fs_cache is not None and save_to_fs:
221 item_raw = json.dumps(item)
222 self.fs_cache.write(fs_key, item_raw)
214 223
215 return item 224 return item
216 225
217 226