[Twisted-Python] Caching mechanism

mardiros mardiros at laposte.net
Sun Nov 15 15:53:12 EST 2009


you can couple a decorator with the maybeDeferred function.
this is a trivial example but it works.
This cache is not cleaned and if you write your decorator in
a class with a __call__ function, you can isolate the cache
dictionary and a configurable lifetime, with a
twisted.internet.task.LoopingCall for this.
In fact, I have already talk about it in a previous message,
too try talking about this technique with more advanced
users than me without answer. I supposed it was not so bad.
Well, I am using now this kind of code to cache sql select
request from a twisted.enterprise.adbapi.ConnectionPool.runQuery
and it's work fine.

from twisted.internet import defer
from twisted.internet import reactor
import types

cache = {}
def memoize(f):
    def g(*args, **kwargs):
        key = (tuple(args), frozenset(kwargs.items()))
        if key not in cache:
            return f(*args, **kwargs)
            print "from cache"
            return cache[key]
    return g

def saveInCache(result,key):
    print "saving to cache"
    cache[key] = result
    return result

def asynchronousIsValidUser(user):
    print "Loading from network"
    d = defer.Deferred()
    reactor.callLater(2, d.callback, user in ["Alice", "Angus", "Agnes"])
    return d.addCallback(saveInCache, ( (user,), frozenset() ))

def printResult(result,user):
    if result:
        print "User", user ,"is authenticated"
        print "User", user ,"is not authenticated"

def authenticateUser(user):
    print "Authenticating user ", user
    d = defer.maybeDeferred(asynchronousIsValidUser, user)
    return d.addCallback(printResult,user)


vitaly at synapticvision.com a écrit :
> Caching the results of some particular method calls, so other server  
> side methods can access those results (I'm thinking about example of  
> global variable per entire server, so once imported, such variable  
> content could be access).
> Quoting exarkun at twistedmatrix.com:
>> On 05:41 pm, vitaly at synapticvision.com wrote:
>>> hi,
>>> is there any cache mechanism for twisted? Could one point me please to
>>> that docs?
>> What sort of caching are you interested in?  Caching the results of
>> method calls?  A caching HTTP proxy?  Caching in the DNS client?  The
>> total list of possibilities could run to many pages. :)
>> Jean-Paul
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list