comparison piecrust/environment.py @ 49:fce061f8c2ed

Fix cache validation issue with rendered segments, limit disk access.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 22 Aug 2014 20:26:05 -0700
parents 558e3602be40
children 2fec3ee1298f
comparison
equal deleted inserted replaced
48:002fa58f54dc 49:fce061f8c2ed
23 def __init__(self, size=2048): 23 def __init__(self, size=2048):
24 self.cache = repoze.lru.LRUCache(size) 24 self.cache = repoze.lru.LRUCache(size)
25 self.lock = threading.RLock() 25 self.lock = threading.RLock()
26 self.fs_cache = None 26 self.fs_cache = None
27 27
28 def get(self, key, item_maker): 28 def get(self, key, item_maker, fs_cache_time=None):
29 item = self.cache.get(key) 29 item = self.cache.get(key)
30 if item is None: 30 if item is None:
31 logger.debug("Acquiring lock for: %s" % key) 31 logger.debug("Acquiring lock for: %s" % key)
32 with self.lock: 32 with self.lock:
33 item = self.cache.get(key) 33 item = self.cache.get(key)
34 if item is None: 34 if item is None:
35 if self.fs_cache is not None: 35 if (self.fs_cache is not None and
36 fs_cache_time is not None):
36 # Try first from the file-system cache. 37 # Try first from the file-system cache.
37 fs_key = _make_fs_cache_key(key) 38 fs_key = _make_fs_cache_key(key)
38 logger.debug("'%s' not found in cache, trying the " 39 if self.fs_cache.isValid(fs_key, fs_cache_time):
39 "file-system: %s" % (key, fs_key)) 40 logger.debug("'%s' found in file-system cache." %
40 try: 41 key)
41 item_raw = self.fs_cache.read(fs_key) 42 item_raw = self.fs_cache.read(fs_key)
42 item = json.loads(item_raw) 43 item = json.loads(item_raw)
43 self.cache.put(key, item) 44 self.cache.put(key, item)
44 return item 45 return item
45 except:
46 pass
47 46
48 # Look into the mem-cache. 47 # Look into the mem-cache.
49 logger.debug("'%s' not found in cache, must build." % key) 48 logger.debug("'%s' not found in cache, must build." % key)
50 item = item_maker() 49 item = item_maker()
51 self.cache.put(key, item) 50 self.cache.put(key, item)