diff --git a/VERSION b/VERSION index cf605f9c..4763a852 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-08-15 14:23:38) dev +Version 2.00.0 (2012-08-15 14:55:51) dev diff --git a/gluon/cache.py b/gluon/cache.py index 437e140e..21b85554 100644 --- a/gluon/cache.py +++ b/gluon/cache.py @@ -175,6 +175,7 @@ class CacheInRam(CacheAbstract): """ dt = time_expire + now = time.time() self.locker.acquire() item = self.storage.get(key, None) @@ -187,14 +188,14 @@ class CacheInRam(CacheAbstract): if f is None: return None - if item and (dt is None or item[0] > time.time() - dt): + if item and (dt is None or item[0] > now - dt): return item[1] - elif item and (item[0] < time.time() - dt) and destroyer: + elif item and (item[0] < now - dt) and destroyer: destroyer(item[1]) value = f() self.locker.acquire() - self.storage[key] = (time.time(), value) + self.storage[key] = (now, value) self.storage[CacheAbstract.cache_stats_name]['misses'] += 1 self.locker.release() return value @@ -345,15 +346,16 @@ class CacheOnDisk(CacheAbstract): if storage: storage.close() + now = time.time() if f is None: return None - if item and (dt is None or item[0] > time.time() - dt): + if item and (dt is None or item[0] > now - dt): return item[1] value = f() storage = self._open_shelf_with_lock() try: - storage[key] = (time.time(), value) + storage[key] = (now, value) storage[CacheAbstract.cache_stats_name] = { 'hit_total': storage[CacheAbstract.cache_stats_name]['hit_total'], diff --git a/gluon/contrib/memcache/__init__.py b/gluon/contrib/memcache/__init__.py index 667691dd..7079e176 100644 --- a/gluon/contrib/memcache/__init__.py +++ b/gluon/contrib/memcache/__init__.py @@ -10,21 +10,19 @@ cache.memcache = MemcacheClient(request,[127.0.0.1:11211],debug=true) import cPickle as pickle import thread +from gluon import current -locker = thread.allocate_lock() +DEFAULT_TIME_EXPIRE = 300 # seconds (must be the same as cache.ram) def MemcacheClient(*a, **b): - locker.acquire() - try: - if not hasattr(MemcacheClient, '__mc_instance'): - MemcacheClient.__mc_instance = _MemcacheClient(*a, **b) - finally: - locker.release() - return MemcacheClient.__mc_instance + if not hasattr(current,'__mc_instance'): + current.__memcache_client = MemcacheClientObj(*a, **b) + return current.__memecache_client -class _MemcacheClient(Client): +class MemcacheClientObj(Client): meta_storage = {} + max_time_expire = 24*3600 def __init__(self, request, servers, debug=0, pickleProtocol=0, pickler=pickle.Pickler, unpickler=pickle.Unpickler, @@ -45,33 +43,44 @@ class _MemcacheClient(Client): else: self.storage = self.meta_storage[app] - - def __call__(self, key, f, time_expire=300): + def __call__(self, key, f, + time_expire=DEFAULT_TIME_EXPIRE, + destroyer = None): if time_expire == None: - time_expire = 10**10 + time_expire = self.max_time_expire # this must be commented because get and set are redefined # key = self.__keyFormat__(key) + now = time.time() value = None - obj = self.get(key) - if obj: - value = obj - elif f is None: - if obj: self.delete(key) - else: + if f is None: + self.delete(key) + elif time_expire==0: value = f() - self.set(key, value, time_expire) + self.set(key, value, self.max_time_expire) + self.set(key+':time()', now, self.max_time_expire) + else: + value = self.get(key) + t0 = self.get(key+':time()') + if value and t0: + if (t0 < now - dt) and destroyer: + destroyer(value) + else: + value = f() + self.set(key, value, self.max_time_expire) + self.set(key+':time()', now, self.max_time_expire) return value - def increment(self, key, value=1, time_expire=300): + def increment(self, key, value=1, time_expire=DEFAULT_TIME_EXPIRE): + """ time_expire is ignored """ newKey = self.__keyFormat__(key) obj = Client.get(self, newKey) if obj: return Client.incr(self, newKey, value) else: - Client.set(self, newKey, value, time_expire) + Client.set(self, newKey, value, self.max_time_expire) return value - def set(self, key, value, time_expire=300): + def set(self, key, value, time_expire=DEFAULT_TIME_EXPIRE): newKey = self.__keyFormat__(key) return Client.set(self, newKey, value, time_expire)