[Twisted-web] New cache proposal

Valentino Volonghi dialtone at gmail.com
Thu May 20 04:07:08 MDT 2004


I've seen taht at least in ten.py and in loaders.py you cache adapters
and templates.

Not too long ago I've written a metaclass that would cache instances
based on what arguments were passed during instantiation.

http://mattgoodall.dyndns.org:8081/105

cache = {}
class MyMetaTemplate(type):
    def __call__(self, *args):
        cached = cache.get(args, None)
        print cache, cached
        if not cached:
            instance = type.__call__(self, *args)
            cache.update({args:instance})
            return instance
        return cached
        
class Pippo(object):
    __metaclass__ = MyMetaTemplate
    template = ''
    def __init__(self, arg1, arg2, arg3):
        self.template = arg1

a = Pippo(1,2,3)
b = Pippo(2,3,4)
c = Pippo(1,2,3)
d = Pippo(2,3,4)
e = Pippo(5,6,7)
f = Pippo(5,6,7)

print id(a), id(b), id(c), id(d), id(e), id(f)

I think the only 'drawback' is that it needs new-style classes, but It
would be easier to use since you don't need extra checking in methods
but once you instantiate you know that the object you are working on
is a new one (if it wasn't cached) or the cached one.

HTH



More information about the Twisted-web mailing list