This is related to the "Threadlocals? Globals?" question and reminded me on
some tests I've done some time ago.

The goal was to cache objects looked up by traversal and reduce database
and filesystem reads.
The objects itself are stored in the traversal tree (the __parent__) just
where they are referenced. In subsequent lookups the cache is checked
before accessing the database.


class Caching:
    """
    Caches loaded objects including data as attributes.
    """
    useCache = True
    expires = 0

    def Cache(self, obj, id):
        """
        """
        if not self.useCache:
            return True
        try:
            lock = thread.allocate_lock()
            lock.acquire(1)
            setattr(self, self._Cachename(id), (obj, time()))
            if lock.locked():
                lock.release()
        except:
            if lock and lock.locked():
                lock.release()
            return False
        return True

    def GetFromCache(self, id=0):
        """
        returns the cached object
        """
        if not self.useCache:
            return None
        n = self._Cachename(id)
        try:
            lock = thread.allocate_lock()
            lock.acquire(1)
            if hasattr(self, n):
                o = getattr(self, n)
                if lock.locked():
                    lock.release()
                return o[0]
        except:
            if lock and lock.locked():
                lock.release()
        return None

    def RemoveCache(self, id):
        """
        """
        if not self.useCache:
            return True
        try:
            lock = thread.allocate_lock()
            lock.acquire(1)
            try:
                delattr(self, self._Cachename(id))
            except:
                pass
            if lock.locked():
                lock.release()
        except:
            if lock and lock.locked():
                lock.release()
        return True

    def _Cachename(self, id):
        return "__c__" + str(id)

I used it with paster and as long as the resource tree structure doesn't
change the class works quite easy and fast.

Basically the caching works on the assumption that the wsgi main() function
is only called once and the root object stored on module level.

root = Root()
def getRoot(request):
    return root

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings,root_factory = getRoot)


Now, would this approach work for the different wsgi implementations? Or is
the main function handled in a different way?


-- 
DV Electric / Arndt Droullier / www.dvelectric.de

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to