That approach works fine as long as you are serving your application in a single process. If you get the performance you need from a single process, this is an easy approach to take. Because of issues with threads in Python, a lot of applications choose to scale by adding processes, in which case you will have trouble invalidating your cache across processes. In that case, something like memcached might be more appropriate. I don't think differences between wsgi implementations will be an issue.
Chris On Thu, Mar 15, 2012 at 6:04 AM, Arndt Droullier <[email protected]>wrote: > 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. > -- 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.
