- October 23, 2008
- middleware cache memcached
- 3 (after 3 ratings)
Request-phase cache middleware that checks to make sure the Cache server is running. Starting it if it is not. This is run for every request, it checks to see if it can get a defined item out of the cache, if that fails it tries to set it. Failing that it decides the server is probably crashed, so goes though and attempts to connect to the server. Failing a connection it will launch a new server.
This is probably not useful on large scale multi server deployments as they likely have their own testing for when services crash, but I am using it in a shared hosting environment where I have to run my own copy of memcache manually and cannot setup proper services testing, so I use this to just make sure the cache server is still running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
import socket import subprocess from django.conf import settings from django.core.cache import cache class CheckCacheServer(object): """ Request-phase cache middleware that checks to make sure the Cache server is running. Starting it if it is not Must be first in the middleware stack so it can check before anything else runs """ def __init__(self): self.cache_backend = settings.CACHE_BACKEND self.servers = self.cache_backend[self.cache_backend.index('//')+2 : self.cache_backend.rindex('/')].split(';') def process_request(self, request): """ This is run for every request, it checks to see if it can get a defined item out of the cache, if that fails it tries to set it Failing that it decides the server is probably crashed, but goes though and attempts to connect to the server. Failing a connection It will launch a new server. """ if not self.cache_backend.startswith('memcached'): return None alive = cache.get("test-connection") if not alive and not cache.set("test-connection", 1, 604800): try: memcached = settings.CACHE_SERVER_STRING except AttributeError: memcached = "memcached -l %s -p %d -c 10 -d" for server in self.servers: ip, port = server.split(':') s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((ip, int(port))) except socket.error, e: # not running, start it subprocess.Popen(memcached % (ip, int(port)), shell=True) finally: s.close() return None
More like this
- Effective content caching for mass-load site using redirect feature by nnseva 5 years, 1 month ago
- Make runfcgi fail when database connection is open before fork by mpasternacki 5 years, 5 months ago
- Caching XHTML render_to_response by smoonen 8 years, 1 month ago
- A smart trace() command by aparajita 7 years, 2 months ago
- Test Server Thread by adamlofts 7 years, 2 months ago