Login

A simple client to get some info (NOT values...) from memcached

Author:
pterk
Posted:
June 17, 2009
Language:
Python
Version:
1.0
Tags:
django cache python memcached
Score:
0 (after 0 ratings)

Simple client to get some stats from memcached.

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
"""
Simple client to get some stats from memcached.

Using some of the *undocumented* (and not guaranteed to remain
working) features you can get a list of keys.

memcached (127.0.0.1:11211): stats
STAT pid 3228
STAT uptime 45498
STAT time 1245270511
STAT version 1.2.2
...

memcached (127.0.0.1:11211): stats items
STAT items:6:number 1
STAT items:6:age 64
STAT items:19:number 1
STAT items:19:age 66
END

memcached (127.0.0.1:11211): stats slabs
STAT 6:chunk_size 252
STAT 6:chunks_per_page 4161
STAT 6:total_pages 1
...
STAT 19:chunk_size 4648
STAT 19:chunks_per_page 225
STAT 19:total_pages 1
...
STAT active_slabs 2
STAT total_malloced 2094372
END

memcached (127.0.0.1:11211): stats cachedump 6 50
ITEM paste|searchreplace|table|safari|fullscreen|directionality|en|advanced [116 b; 1245270494 s]
END

memcached (127.0.0.1:11211): stats cachedump 19 50
ITEM contentmanager:9:1:content [3940 b; 1245270492 s]
END

"""

import memcache
import readline

PROMPT = "memcached (%s): "

MESSAGE_ENDINGS = ["END", "ERROR", "SERVER_ERROR", "CLIENT_ERROR","OK","DELETED","NOT FOUND"]

# These (can) return binary data. Use a proper client to get those ;-)
UNSUPPORTED_COMMANDS = ["get", "gets", "set", "add", "replace", 
                        "append", "prepend", "cas","incr","decr"]

def message_ends(command, output):
    if command == "version" and output!="":
        return True
    else:
        if output=="":
            return False
        return output.split()[0] in MESSAGE_ENDINGS

def repl(cache_host):
    h = memcache._Host( cache_host )
    h.connect()
    
    while True:
        try:
            command = str(raw_input(PROMPT % cache_host))
        except EOFError:
            print "bye"
            break
        if not command:
            continue
        if command.split()[0] in UNSUPPORTED_COMMANDS:
            print "Not supported"
            continue
        if command == "quit":
            break
        h.send_cmd(command)
        l = "" ;
        while not message_ends(command, l):
            l = h.readline()
            print l

    h.close_socket()

if __name__ == "__main__":
    import sys
    import re
    try:
        # Use with django project
        from settings import CACHE_BACKEND
        if not CACHE_BACKEND.startswith( "memcached://" ):
            print "You have not configured memcached as your django cache backend"
            sys.exit(1)
        else:
            m = re.search( r"//(.+:\d+)", CACHE_BACKEND )
            cache_host =  m.group(1)
            repl(cache_host)
    except (ImportError, KeyError):
        # Try to use the first argument as cache_host
        try:
            repl(sys.argv[1])
        except IndexError:
            print 
            print "Usage: %s host:port" % sys.argv[0]
            sys.exit(1)

More like this

  1. JSON instead of pickle for memcached by diverman 3 years, 1 month ago
  2. Property Attributes in Memcache by ori 3 years, 4 months ago
  3. MintCache (simple version) by disqus 6 years, 8 months ago
  4. AutoSlugField by callipeo 7 years, 3 months ago
  5. Method Caching by bryanhelmig 3 years, 8 months ago

Comments

Please login first before commenting.