Test and Restart Memcached Server

 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

  1. Effective content caching for mass-load site using redirect feature by nnseva 10 months, 3 weeks ago
  2. Check Size of Object in memcached by deryck 4 years, 6 months ago
  3. Per-site vary cache on language by fneumann 4 years, 7 months ago
  4. MintCache by gfranxman 5 years, 1 month ago
  5. MintCache (simple version) by disqus 3 years, 11 months ago

Comments

(Forgotten your password?)