-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmc_util.py
executable file
·91 lines (74 loc) · 1.62 KB
/
mc_util.py
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
#!/usr/bin/env python
#
# $Id: util.py Exp $
#
# Util functions for memcache
import memcache, sys, string
usage = '1) mc_util.py get <key> \n2) mc_util.py set <key> <val> \n3) mc_util.py set <key> <val> <ttl>'
# memcache server
MC_HOSTLIST=['localhost:11211']
# 120 min timeout for memcache
TTL = 120 * 60
mc = None
verbose = 0
# replace dots with underscores for memcache. shortname is not really correct. change this.
def shortname(hostname):
return hostname.replace(".","_")
# return hostname.split('.')[0]
def longname(hostname):
return hostname.replace("_",".")
def mc_init():
global mc
if not mc:
mc = memcache.Client(MC_HOSTLIST)
def mc_get(key):
mc_init()
try:
r = mc.get(key)
except:
r = None
if verbose:
print "GET", key, r
return r
def mc_set(key, val, ttl=TTL):
mc_init()
try:
mc.set(key, val, ttl)
except:
pass
if verbose:
print "SET", key, val
def mc_get_multi(k):
mc_init()
try:
d = mc.get_multi(k)
except exception, e:
if verbose > 1:
print e
pass
if verbose:
print "GET multi", d
return d
def mc_set_multi(d, ttl=TTL):
mc_init()
try:
mc.set_multi(d, ttl)
except:
pass
if verbose:
print "SET multi", d
# interactive use
if ( len(sys.argv) > 2 ):
op = sys.argv[1]
k = sys.argv[2]
if op == 'get' :
print mc_get(k)
elif op == 'set' :
v = sys.argv[3]
if ( len(sys.argv) == 5 ):
t = sys.argv[4]
mc_set(k, v, t)
else:
mc_set(k, v)
else:
print usage