[Twisted-Python] Caching mechanism

mardiros mardiros at laposte.net
Mon Nov 16 04:28:24 EST 2009


Here is the main part of my code

class CacheCleanerBaseMixin:
def __init__(self, cachetimer,cachelifetime):
self._cache = {}
self._cachelifetime = timedelta(0,cachelifetime)
self._cleaner = task.LoopingCall(self.cleanCache)
self._cleaner.start(cachetimer,now=False)
self._cleanrun = False

def cleanCache(self):
if not self._cleanrun:
self._cleanrun = True
log.msg('Clean the cache...', logLevel=logging.DEBUG)
expire = datetime.today()
i = 0
for k,v in self._cache.items():
if v[0] < expire:
i+=1
del self._cache[k]
log.msg('%i items removed' % i, logLevel=logging.DEBUG)
self._cleanrun = False


class asyncmemoize(CacheCleanerBaseMixin):
def __init__(self, function,cachetimer=30,cachelifetime=60):
CacheCleanerBaseMixin.__init__(self, cachetimer,cachelifetime)
self.function = function

def __call__(self, *args, **kwargs):
key = (tuple(args), frozenset(kwargs.items()))
if key not in self._cache:
return self.function(SqlConnection(),*args, **kwargs).addCallback(self.callback,key)
else:
log.msg('Retrievieng the data from the cache',logLevel=logging.DEBUG)
return self._cache[key][1]

def callback(self,result,key):
try:
self._cache[key] = [datetime.today() + self._cachelifetime, result]
finally:
return result




class SqlConnection(object): 

_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance

def configure(self, dbdriver, dbdriverargs,
log_request=False,
cache_file='sqlcache.shelve',
pool_file='sqloperation.shelve',
pool_maxattempt=5,
pool_time=30):
self.log_request = log_request
self.pool = adbapi.ConnectionPool(dbdriver,**dbdriverargs) 

@asyncmemoize
def internal_runQuery(self,query,args=None):
rv = None
try:
if args:
rv = self.pool.runQuery(query,args)
else:
rv = self.pool.runQuery(query)
except:
log.err() 
return rv

def runQuery(self,query,args=None):
try:
d = defer.maybeDeferred(self.internal_runQuery, query,args)
return d
except:
log.err() 


---------------------------------------------------------------------------- 
Laposte.net fête ses 10 ans ! 

Gratuite, garantie à vie et déjà utilisée par des millions d'internautes... 
vous aussi, pour votre adresse e-mail, choisissez laposte.net. 

Laposte.net, bien + qu'une messagerie 
----------------------------------------------------------------------------




More information about the Twisted-Python mailing list