public
Description: k9 is a complete Django based Project for my k9 rescue dog unit. See Wiki for details.
Homepage: http://www.rettungshunde-stralsund.de/
Clone URL: git://github.com/bartTC/k9.git
k9 / cache_status.py
100644 59 lines (48 sloc) 1.449 kb
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
49
50
51
52
53
54
55
56
57
58
59
from django import http
from django.shortcuts import render_to_response
from django.conf import settings
from django.views.decorators.cache import never_cache
 
import datetime, re
 
@never_cache
def cache_status(request):
    try:
        import memcache
    except ImportError:
        raise http.Http404
 
    if not (request.user.is_authenticated() and
            request.user.is_staff):
        raise http.Http404
 
    # get first memcached URI
    m = re.match(
        "memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
    )
    if not m:
        raise http.Http404
 
    host = memcache._Host(m.group(1))
    host.connect()
    host.send_cmd("stats")
 
    class Stats:
        pass
 
    stats = Stats()
 
    while 1:
        line = host.readline().split(None, 2)
        if line[0] == "END":
            break
        stat, key, value = line
        try:
            # convert to native type, if possible
            value = int(value)
            if key == "uptime":
                value = datetime.timedelta(seconds=value)
            elif key == "time":
                value = datetime.datetime.fromtimestamp(value)
        except ValueError:
            pass
        setattr(stats, key, value)
 
    host.close_socket()
 
    return render_to_response(
        'cache_status.html', dict(
            stats=stats,
            hit_rate=100 * stats.get_hits / stats.cmd_get,
            time=datetime.datetime.now(), # server time
        ))