def memcache_cache(f, *args, **kwargs): # check if this set of args / kwargs / f is in memcache c = memcache_client.get('key_counter') or 0 # create our key using function name and args key = '%s_%s_%s_%s' % (c,f.__name__, '_'.join([str(x) for x in args if isinstance(x,(basestring,unicode))]), '_'.join(['%s.%s' % (x,kwargs[x]) for x in sorted(kwargs.keys())])) key = key.replace(' ','') # fingers crossed cherrypy.log('memcache key: %s' % key) r = memcache_client.get(key) # miss =/ if not r: r = f(*args,**kwargs) # update memcache cr = zlib.compress(r) cherrypy.log('writing compressed to memcache %s:%s' % (len(r),len(cr))) memcache_client.set(key,cr) else: r = zlib.decompress(r) cherrypy.log('memcache hit') return r
return to_return # we want to make sure and listen on commits, if a commit # happens we need to increment the memcache key counter def increment_memcache_counter(*args,**kwargs): cherrypy.log('incrementing memcache') memcache_client.incr("key_counter") # add the listener event.listen(m.session, "after_commit", increment_memcache_counter) # make sure the counter exists r = memcache_client.get('key_counter') if not r: memcache_client.set('key_counter',0) else: memcache_client.incr('key_counter') def node_most_recent_relative_update(node_datas): # go down through the relatives, find the newest one def c(nds): newest = nds.get('epoch_updated_at') for node_data in nds.get('_relatives',[]): f = c(node_data) if f > newest: newest = f return newest newest = c(node_datas) if not newest: return 0