Specifying a timeout when fetching / placing data in memcache (django)
I have a django based http server and I am using django.core.cache.backends.memcached.MemcachedCache as a client library to access memcache. I want to know if we can set a timeout or something (say 500ms) so that the memcached call will return False if it can't access the cache for 500ms. and we make a call to the database. Is there such a setting for this?
source to share
Haven't tried this before, but you can use streams and set a timeout for the function call in the cache. As an example, ignore the example provided in the main body at this link, but take a look at Jim Carroll's comment:
http://code.activestate.com/recipes/534115-function-timeout/
Adapted for use:
from threading import Timer
import thread, time, sys
def timeout():
thread.interrupt_main()
try:
Timer(0.5, timeout).start()
cache.get(stuff)
except:
print "Use a function to grab it from the database!"
I don't have time to test it out right now, but I will be worried about whether Django is threading itself, and if so, interrupts the main thread, what do you really want to do? Either way, this is a potential starting point. I was looking for a config option that would allow this and found nothing.
source to share