3

I have a django based http server and I use django.core.cache.backends.memcached.MemcachedCache as the client library to access memcache. I want to know whether we can set a timeout or something (say 500ms.) so that the call to memcached returns False if it is not able to access the cache for 500ms. and we make the call to the DB. Is there any such setting to do that?

Rajat
  • 1,766
  • 2
  • 21
  • 42

1 Answers1

0

Haven't tried this before, but you may be able to use threading and set up a timeout for the function call to cache. As an example, ignore the example provided in the main body at this link, but look at Jim Carroll's comment:

http://code.activestate.com/recipes/534115-function-timeout/

Adapted for something you might 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 right now, but my concern would be whether Django itself is threaded, and if so, is interrupting the main thread what you really want to do? Either way, it's a potential starting point. I did look for a configuration option that would allow for this and found nothing.

  • It will indeed have problems with django because it is not threaded. I was looking at the code and the socket timeout is specified in the python memcache client (memcache.py) with _SOCKET_TIMEOUT. The default is set to 3 seconds. That is a bit too high for me. Probably I will have to modify this value. But it is really strange that the python memcache client does not provide any way to customize this timeout. – Rajat Apr 03 '12 at 20:03
  • YES, you can specify the timeout. pass the parameter socket_timeout into memcache's Client(), as in: from memcache import Client; c = Client(['localhost'], socket_timeout=10.5) – Kevin J. Rice Nov 04 '13 at 22:05