pymemcache get start
Getting started!
A comprehensive, fast, pure-Python memcached client library.
Basic Usage
from pymemcache.client.base import Client
client = Client(('localhost', 11211))
client.set('some_key', 'some_value')
result = client.get('some_key')
Using a memcached cluster
This will use a consistent hashing algorithm to choose which server to set/get the values from. It will also automatically rebalance depending on if a server goes down.
from pymemcache.client.hash import HashClient client = HashClient([
('127.0.0.1', 11211),
('127.0.0.1', 11212)
])
client.set('some_key', 'some value')
result = client.get('some_key')
Serialization
import json
from pymemcache.client.base import Client def json_serializer(key, value):
if type(value) == str:
return value, 1
return json.dumps(value), 2 def json_deserializer(key, value, flags):
if flags == 1:
return value
if flags == 2:
return json.loads(value)
raise Exception("Unknown serialization format") client = Client(('localhost', 11211), serializer=json_serializer,
deserializer=json_deserializer)
client.set('key', {'a':'b', 'c':'d'})
result = client.get('key')
pymemcache provides a default pickle-based serializer:
from pymemcache.client.base import Client
from pymemcache import serde class Foo(object):
pass client = Client(('localhost', 11211),
serializer=serde.python_memcache_serializer,
deserializer=serde.python_memcache_deserializer)
client.set('key', Foo())
result client.get('key')
The serializer uses the highest pickle protocol available. In order to make sure multiple versions of Python can read the protocol version, you can specify the version with get_python_memcache_serializer
client = Client(('localhost', 11211),
serializer=serde.get_python_memcache_serializer(pickle_version=2),
deserializer=serde.python_memcache_deserializer)
Deserialization with python3
def json_deserializer(key, value, flags):
if flags == 1:
return value.decode('utf-8')
if flags == 2:
return json.loads(value.decode('utf-8'))
raise Exception("Unknown serialization format")
Key Constraints
This client implements the ASCII protocol of memcached. This means keys should not contain any of the following illegal characters: > Keys cannot have spaces, new lines, carriage returns, or null characters. We suggest that if you have unicode characters, or long keys, you use an effective hashing mechanism before calling this client. At Pinterest, we have found that murmur3 hash is a great candidate for this. Alternatively you can set allow_unicode_keys to support unicode keys, but beware of what unicode encoding you use to make sure multiple clients can find the same key.
Best Practices
- Always set the connect_timeout and timeout arguments in the :py:class:`pymemcache.client.base.Client` constructor to avoid blocking your process when memcached is slow. You might also want to enable the no_delay option, which sets the TCP_NODELAY flag on the connection's socket.
- Use the "noreply" flag for a significant performance boost. The "noreply" flag is enabled by default for "set", "add", "replace", "append", "prepend", and "delete". It is disabled by default for "cas", "incr" and "decr". It obviously doesn't apply to any get calls.
- Use get_many and gets_many whenever possible, as they result in less round trip times for fetching multiple keys.
- Use the "ignore_exc" flag to treat memcache/network errors as cache misses on calls to the get* methods. This prevents failures in memcache, or network errors, from killing your web requests. Do not use this flag if you need to know about errors from memcache, and make sure you have some other way to detect memcache server failures.
pymemcache get start的更多相关文章
- 使用Python操作memcache
Python连接memcached的库有很多,处于简单以及高效的原则,最终选择了pymemcache, 优点 完全实现了memcached text协议 对于send/recv操作可以配置timeou ...
- flask可以通过缓存模板或者页面达到性能提升
flask可通过插件flask-cache缓存页面,或者把模板缓存到memcache里,增加访问速度. 前提是:页面不是频繁变化的.如果你的访问量很大的话,哪怕缓存一两分钟也会大大的提高性能的 Fla ...
- Memcached使用总结之:使用Python操作memcache
Python连接memcached的库有很多,处于简单以及高效的原则,最终选择了pymemcache,优点完全实现了memcached text协议对于send/recv操作可以配置timeout支持 ...
- python 操作memercache类库
pip install python-memcached pip install pymemcache pip install python-libmemcached
- memcache 使用手册
Memcached 教程 Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fit ...
- python浅学【网络服务中间件】之Memcached
一.缓存的由来: 提升性能 绝大多数情况下,select 是出现性能问题最大的地方.一方面,select 会有很多像 join.group.order.like 等这样丰富的语义,而这些语义是非常耗性 ...
随机推荐
- Cmder使用总结
windows cmd 使用不方便之处: 1.窗口size不能便捷缩放 2.复制文本,不能直接用鼠标拷贝,还需要多一道菜单操作:而且,还只能块状拷贝,而不是按行字符,极其不便 3.不支持多Tab页,多 ...
- Http、TCP/IP、Socket的区别
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 14915 Ac ...
- python之-微信开发学习
微信公众平台技术文档https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432# 注意,最好以python3 运行,中文 ...
- LINUX下安装和配置WEBLOGIC10.0.3
weblogic for linux安装 首先声明,我参考了某位原创者的笔记,加以整理的.安装1. 安装前的准备工作1.1 首先请确认您要安装的Weblogic版本所在的平台已通过了BEA的认证,完整 ...
- openwrt下载编译
1. 安装依赖包: yum install autoconf binutils bison bzip2 flex gawk gcc gcc-c++ gettext make ncurses-devel ...
- 2003 -Can't connection to mysql server on | navicat for mysql Access denied for user 'root'@''ip'(using password :yes)
用本机windows上的Navicat for mysql链接虚拟机Linux的mysql数据库时,第一次连接的时候报的错误是 2003 -Can't connection to mysql serv ...
- Python使用django搭建web开发环境
安装 Python 去 Python 官方网站找到 Python 3 的下载地址,根据你的系统选择 32 位或者 64 位的安装包,下载好后双击安装即可. 检测是否安装完成 C:\WINDOWS\sy ...
- Visual Studio VS如何卸载Visual assistant
1 点击工具-扩展管理器 2 选中Visual Assist X,点击卸载即可.
- Javascript 运行机制
先看一下下面这段js代码: console.log('1'); setTimeout(function(){ console.log('2'); },0); console.log('3'); 请问打 ...