本文整理汇总了Python中pymemcache.client.base.Client.set方法的典型用法代码示例。如果您正苦于以下问题:Python Client.set方法的具体用法?Python Client.set怎么用?Python Client.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymemcache.client.base.Client
的用法示例。
在下文中一共展示了Client.set方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Reader
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
class Reader(BaseReader):
"""
Memcached settings Reader
A simple memcached getter using pymemcache library.
"""
_default_conf = {
'host': 'localhost',
'port': 11211
}
def __init__(self, conf):
super(Reader, self).__init__(conf)
self.client = Client((self.conf['host'], self.conf['port']))
def _get(self, key):
result = self.client.get(key)
if isinstance(result, six.binary_type):
result = result.decode('utf-8')
return result
def _set(self, key, value):
self.client.set(key, value, noreply=False)
示例2: MemcachedCache
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
class MemcachedCache(CachualCache):
"""A cache using `Memcached <https://memcached.org/>`_ as the backing
cache. The same caveats apply to keys and values as for Redis - you should
only try to store strings (using the packing/unpacking functions). See the
documentation on Keys and Values here:
:class:`pymemcache.client.base.Client`.
:type host: string
:param host: The Memcached host to use for the cache.
:type port: integer
:param port: The port to use for the Memcached server.
:type kwargs: dict
:param kwargs: Any additional args to pass to the :class:`CachualCache`
constructor.
"""
def __init__(self, host='localhost', port=11211, **kwargs):
super(MemcachedCache, self).__init__(**kwargs)
self.client = MemcachedClient((host, port))
def get(self, key):
"""Get a value from the cache using the given key.
:type key: string
:param key: The cache key to get the value for.
:returns: The value for the cache key, or None in the case of cache
miss.
"""
return self.client.get(key)
def put(self, key, value, ttl=None):
"""Put a value into the cache at the given key. For constraints on keys
and values, see :class:`pymemcache.client.base.Client`.
:type key: string
:param key: The cache key to use for the value.
:param value: The value to store in the cache.
:type ttl: integer
:param ttl: The time-to-live for key in seconds, after which it will
expire.
"""
if ttl is None:
ttl = 0
self.client.set(key, value, expire=ttl)
示例3: test_incr_decr
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
def test_incr_decr(client_class, host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
client.flush_all()
result = client.incr(b'key', 1, noreply=False)
assert result is None
result = client.set(b'key', b'0', noreply=False)
assert result is True
result = client.incr(b'key', 1, noreply=False)
assert result == 1
def _bad_int():
client.incr(b'key', b'foobar')
with pytest.raises(MemcacheClientError):
_bad_int()
result = client.decr(b'key1', 1, noreply=False)
assert result is None
result = client.decr(b'key', 1, noreply=False)
assert result == 0
result = client.get(b'key')
assert result == b'0'
示例4: test_serialization_deserialization
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
def test_serialization_deserialization(host, port, socket_module):
def _ser(key, value):
return json.dumps(value).encode('ascii'), 1
def _des(key, value, flags):
if flags == 1:
return json.loads(value.decode('ascii'))
return value
client = Client((host, port), serializer=_ser, deserializer=_des,
socket_module=socket_module)
client.flush_all()
value = {'a': 'b', 'c': ['d']}
client.set(b'key', value)
result = client.get(b'key')
assert result == value
示例5: Memcached
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
class Memcached(object):
DELAY = 0.5
DEBUG = False
def __init__(self, hostname, port, **params):
self.mc = Client((hostname, port))
def handle(self, topic, message):
"""
"""
if 'cmd' not in message:
raise Exception("Bad message: no command")
cmd = message['cmd']
if not hasattr(self, cmd):
raise Exception("Unknown command: " + cmd)
tryit = True
while tryit:
tryit = False
try:
getattr(self, cmd)(message)
except MemcacheUnexpectedCloseError:
# Server dropped dead - we'll retry
tryit = True
except IOError:
# Something network-related - retry
tryit = True
if tryit:
time.sleep(self.DELAY)
def set(self, message):
text = message['val'].encode('utf-8')
if message.get('sbt', None):
purge_time = time.time() + message.get('uto', 0)
text = text.replace('$UNIXTIME$', '%.6f' % purge_time)
if self.DEBUG:
print("Set {0}-{1}-{2}".format(message['key'].encode('utf-8'), text, int(message['ttl'])))
self.mc.set(message['key'].encode('utf-8'), text, int(message['ttl']))
def delete(self, message):
self.mc.delete(message['key'])
示例6: Client
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
import time
import urllib2
from PIL import Image
from pymemcache.client.base import Client
client = Client(('127.0.0.1', 11211))
url = "http://127.0.0.1:5080/index1.jpg"
i = 0
try:
while True:
try:
input = urllib2.urlopen(url)
input.readline(); input.readline()
content_length = int(input.readline().split(": ")[1].strip())
input.readline(); input.readline()
data = input.read(content_length)
client.set(str(i), data, 30)
client.set('curno', i)
i = i + 1
time.sleep(0.7)
except Exception, e:
print e.__doc__
print e.message
sys.exit(1)
except KeyboardInterrupt:
sys.exit(1)
示例7: gao_memcache
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
def gao_memcache():
client = Client((app.config['MEMCACHE_HOST'], app.config['MEMCACHE_PORT']))
client.set('some_key', 'some_value', expire=10)
result = client.get('some_key')
print result
示例8: getscreenimage
# 需要导入模块: from pymemcache.client.base import Client [as 别名]
# 或者: from pymemcache.client.base.Client import set [as 别名]
m, n = getscreenimage()
return m, n
#------------------------
#logging.basicConfig(
# format='# %(levelname)s: %(message)s',
# level=logging.DEBUG,
#)
client = Client(('127.0.0.1', 11211))
try:
result = client.get('appletv')
if result:
sys.exit("time is not passed")
else:
print ("appletv is on")
# get current channel and change input to HDMI2
# tv should be on
m, n = getCHandHDMI2()
if m != n:
changeinout(m, n)
else:
handleCommand("20")
client.set('appletv', 'true', 60)
except Exception, e:
print e.__doc__
print e.message
sys.exit(1)