本文整理汇总了Python中memcache.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: whereyoulive_sum
def whereyoulive_sum():
global addresses
ret = "<table class=\"normal\">"
ret += "<th class=\"normal\">" + "Address" + "</th>"
ret += "<th class=\"normal\">" + "Total/Address" + "</th>"
conn = Client([gethostname()+':11211'])
vals = []
for k in addresses:
ret += "<tr class=\"normal\">"
ret += "<td class=\"normal\">" + k + "</td>"
val = conn.get(addr_prefix+k)
if val == None:
vals.append(0)
else:
vals.append(val)
ret += "<td class=\"normal\"><span>" + str(vals[-1]) + "</span></td>"
ret += "</tr>"
ret += "<tr align=\"center\"><td class=\"normal\">" + "Sum" + "</td>"
if len(vals) == 0:
ret += "<td class=\"normal\">" + "0" + "</td></tr>"
else:
ret += "<td class=\"normal\">" + str(reduce(lambda i, j : i+j, [i for i in vals])) + "</td></tr>"
ret += "</table>"
return ret
示例2: remove
def remove(self, layer, coord, format):
""" Remove a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision)
mem.delete(key)
示例3: MemcachedZipState
class MemcachedZipState(object):
"""get or set state of an zip job.
"""
def __init__(self, uid):
self._uid = uid
if ZIPSTATE_MEMCACHEDSERVER not in os.environ:
raise ValueError(
'Expect environment variable "{0}: pointing a memcached '
'server in order to share state '
'information.'.format(ZIPSTATE_MEMCACHEDSERVER)
)
self._client = Client([os.environ[ZIPSTATE_MEMCACHEDSERVER]])
def _combined_key(self, key):
return '{0}-{1}'.format(self._uid, key)
def __getitem__(self, key):
"""get state of zip job
"""
return self._client.get(self._combined_key(key))
def __setitem__(self, key, value):
"""set state of zip job
"""
return self._client.set(self._combined_key(key), value)
示例4: SocketHandler
class SocketHandler(websocket.WebSocketHandler):
client_infos = defaultdict()
def __init__(self, *args, **kwargs):
super(SocketHandler, self).__init__(*args, **kwargs)
servers = ['127.0.0.1:11211']
self._mc = Client(servers)
def open(self):
self.client_infos[self] = ClientInfo()
def on_close(self):
del self.client_infos[self]
def on_message(self, message):
now = datetime.now()
delta = now - self.client_infos[self].prev_message_time
# TODO: Restrict access based on delta.
time = self._mc.get('time')
if self.client_infos[self].prev_image_time == time:
self.write_message({'timeout': 50})
else:
image = self._mc.get('image')
image = base64.b64encode(image)
self.write_message({'image': image, 'timeout': 80})
self.client_infos[self].prev_image_time = time
self.client_infos[self].prev_message_time = now
def check_origin(self, origin):
return True
示例5: MemcacheStoreTest
class MemcacheStoreTest(unittest.TestCase, SimpleKVTest):
def setUp(self):
self.mc = Client([testconf.get('memcache', 'server')])
self.mc.flush_all()
self.store = MemcacheStore(self.mc)
def tearDown(self):
self.mc.flush_all()
def test_memcache_connection(self):
pass
# disabled tests (not fully API support for memcache)
test_has_key = None
test_has_key_with_delete = None
test_key_iterator = None
test_keys = None
def test_keys_throws_io_error(self):
with self.assertRaises(IOError):
self.store.keys()
with self.assertRaises(IOError):
self.store.iter_keys()
with self.assertRaises(IOError):
iter(self.store)
def test_contains_throws_io_error_or_succeeds(self):
try:
'a' in self.store
except IOError:
pass
示例6: save
def save(self, body, layer, coord, format):
""" Save a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision)
mem.set(key, body, layer.cache_lifespan or 0)
示例7: __init__
class FoursquareAPI:
url_base = "https://api.foursquare.com/v2/%s"
payload = {}
mc = False
mcprefix = False
def __init__(self, lifestream):
OAUTH_FILENAME = lifestream.config.get("foursquare", "secrets_file")
CONSUMER_KEY = lifestream.config.get("foursquare", "client_id")
CONSUMER_SECRET = lifestream.config.get("foursquare", "secret")
MEMCACHE_HOST = lifestream.config.get("memcache", "host")
MEMCACHE_PORT = lifestream.config.get("memcache", "port")
self.mcprefix = lifestream.config.get("memcache", "prefix")
servers = ["%s:%s" % (MEMCACHE_HOST, MEMCACHE_PORT)]
self.mc = Client(servers, debug=1)
if not os.path.exists(OAUTH_FILENAME):
logger.error("No OAUTH found at %s" % OAUTH_FILENAME)
raise Exception("You need to run foursquare_oauth.py to generate the oauth key")
oauth_token, oauth_token_secret = read_token_file(OAUTH_FILENAME)
self.payload = {
'v': "20170801",
'oauth_token' : oauth_token
}
def cache_get(self, url, params):
m = hashlib.sha224()
m.update(url)
m.update(str(params))
key = m.hexdigest()
res = self.mc.get(key)
if(res):
return json.loads(res)
r = requests.get(self.url_base % "users/self/checkins", params=self.payload)
self.mc.set(key, json.dumps(r.json()))
return r.json()
def my_checkins(self):
return self.cache_get(self.url_base % "users/self/checkins", params=self.payload)
def search_near(self, lat, lng, intent="checkin", radius=50, limit=10):
payload = self.payload
payload['ll'] = "%s,%s" % (lat, lng)
payload['intent'] = intent
payload['radius'] = radius
payload['limit'] = limit
return self.cache_get(self.url_base % "venues/search", params=self.payload)
示例8: remove
def remove(self, layer, coord, format):
""" Remove a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
mem.delete(key)
mem.disconnect_all()
示例9: save
def save(self, body, layer, coord, format):
""" Save a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
mem.set(key, body, layer.cache_lifespan or 0)
mem.disconnect_all()
示例10: unlock
def unlock(self, layer, coord, format):
""" Release a cache lock for this tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
mem.delete(key + "-lock")
mem.disconnect_all()
示例11: preset
def preset(sample):
global addresses
conn = Client([gethostname()+':11211'])
if conn.get_stats()[0][1]['curr_items'] == 0:
for k in addresses:
conn.set(addr_prefix+k, 0)
示例12: read
def read(self, layer, coord, format):
""" Read a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
value = mem.get(key)
mem.disconnect_all()
return value
示例13: save
def save(self, body, layer, coord, format):
""" Save a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
if body is not None:
body = b64encode(body).decode('ascii')
mem.set(key, body, layer.cache_lifespan or 0)
mem.disconnect_all()
示例14: read
def read(self, layer, coord, format):
""" Read a cached tile.
"""
mem = Client(self.servers)
key = tile_key(layer, coord, format, self.revision, self.key_prefix)
value = mem.get(key)
mem.disconnect_all()
if value is None:
return None
return b64decode(value.encode('ascii'))
示例15: WrappedClient
class WrappedClient(object):
def __init__(self, *args):
self.args = args
self.mc = Client(*args, cache_cas=True, socket_timeout=10)
self.del_que = []
import threading
def gets(self, key):
while True:
result = self.mc.gets(key)
if isinstance(result, tuple):
return result[0]
return result
def cas(self, key, value):
retry_count = 0
try:
while True:
result = self.mc.cas(key, value)
if not isinstance(result, bool):
if retry_count <= 10:
retry_count += 1
wait_time = 0.001 * randint(0, 1 << retry_count)
print "add fail, retry for sleep"
sleep(wait_time)
self.mc = Client(*self.args, cache_cas=True, socket_timeout=10)
continue
# raise ConnectionError
return result
except TypeError:
return False
def add(self, key, value):
retry_count = 0
while True:
result = self.mc.add(key, value)
if not isinstance(result, bool):
if retry_count <= 10:
retry_count += 1
wait_time = 0.001 * randint(0, 1 << retry_count)
print "add fail, retry for sleep"
sleep(wait_time)
print self.args
self.mc = Client(*self.args, cache_cas=True, socket_timeout=10)
continue
# raise ConnectionError
return result
# delegation
def __getattr__(self, attrname):
return getattr(self.mc, attrname)