This page is for memcache tips and tricks
An Item of Interest: There is a posting on web2py forum that there is a missing parameter bug in a call to the memcache API. http://groups.google.com/group/web2py/browse_thread/thread/fcac81ee1b713dbe
Sounds like we need a 'lint' tool for python which can catch missing args. This bug report posting also has an interesting snippet of memcache api code, so that is very instructive to read.
Here is the code snippet from that posting:
class MemcacheClient(Client):
def __init__(self, request, servers, debug=0, pickleProtocol=0,
pickler=pickle.Pickler, unpickler=pickle.Unpickler,
pload=None, pid=None):
self.request=request
Client.__init__(self,servers,debug,pickleProtocol,
pickler,unpickler,pload,pid)
def __call__(self,key,f,time_expire=300):
key='%s/%s' % (self.request.application,key)
dt=time_expire
value=None
obj=self.get(key)
if obj and obj[0]>time.time()-dt:
value=obj[1]
elif f is None:
if obj: self.delete(key)
else:
value=f()
self.set(key,(time.time(),value))
return value
def increment(self,key,value=1):
key='%s/%s' % (self.request.application,key)
obj=self.get(key)
if obj: value=obj[1]+value
self.set(key,(time.time(),value))
return value
It does not look to be thread safe. If some asynchronous Ajax calls came in from the browser which try to use this cache, there could be trouble.
Here is a posting " Memcache & SQL Query as key " which shows an example of using Memcache
http://groups.google.com/group/web2py/browse_thread/thread/81aefcf711b21c6c
From http://groups.google.com/group/web2py/browse_thread/thread/81aefcf711b21c6c
From: NicolasB <nicolas.bn...@gmail.com>
Date: Wed, 6 May 2009 08:45:21 -0700 (PDT)
Local: Wed, May 6 2009 11:45 am
Subject: Memcache & SQL Query as key
Hello,
I am using memcache to hold query directly:
db((db.ad...,cache=(cache.mem,100))
Web2py creates a key with the query. However memcache does not seem to
support spaces.
I modified web2py_src_1.61.4\web2py\gluon\contrib\memcache\__init__.py
to format the key
I don't know if this is the best way to do...
Best regards,
Nicolas
from gluon.contrib.memcache.memcache import Client
import time
"""
example of usage:
cache.memcache=MemcacheClient(request,[127.0.0.1:11211],debug=true)
"""
import cPickle as pickle
class MemcacheClient(Client):
def __init__(self, request, servers, debug=0, pickleProtocol=0,
pickler=pickle.Pickler, unpickler=pickle.Unpickler,
pload=None, pid=None):
self.request=request
Client.__init__(self,servers,debug,pickleProtocol, pickler,unpickler,pload,pid)
def __call__(self,key,f,time_expire=300):
key=self.__keyFormat__(key)
dt=time_expire
value=None
obj=self.get(key)
if obj and obj[0]>time.time()-dt:
value=obj[1]
elif f is None:
if obj: self.delete(key)
else:
value=f()
self.set(key,(time.time(),value))
return value
def increment(self,key,value=1):
key=self.__keyFormat__(key)
obj=self.get(key)
if obj: value=obj[1]+value
self.set(key,(time.time(),value))
return value
def __keyFormat__(self,key):
return '%s/%s' % (self.request.application,key.replace(' ','_'))