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 等这样丰富的语义,而这些语义是非常耗性 ...
随机推荐
- HDU-2159 FATE,01背包变形
FATE 这道题和完全背包十分类似,只不过加上了忍耐度这个条件限制,所以很正常的想到用三维数组来模拟.但背包问题优化只有一层循环,这里当然也可以把种类这一层省略.d[i][j]表示杀i只怪耗费忍耐度为 ...
- Windows的Java_HOME环境变更配置
Windows的Java_HOME环境变更配置 一般会配置三个环境变更: 1.系统变量 变量名JAVA_HOME 变量值=安装目录,如:C:\Program Files (x86)\Java\jdk ...
- mysql 修改管理员密码
mysql 修改管理员密码 本次学习环境: windows 7系统.mysql 5.7.14. 一.如果是忘记了用户密码: (1).关闭正在运行的MySQL服务. 方法一:可以直接操作wamp软件,左 ...
- [Bzoj4540][Hnoi2016] 序列(莫队 + ST表 + 单调队列)
4540: [Hnoi2016]序列 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1567 Solved: 718[Submit][Status] ...
- CodeForces 570D 【dfs序】
题意: 给一颗树,根节点深度为1,每一个节点都代表一个子母. 数据输入: 节点数 询问数 从编号为2的节点开始依次输入其父节点的编号(共有节点数减1个数字输入) 字符串有节点数个小写字母 接下来询问 ...
- Python的环境变量设置
python安装完成后,它的配置很简单,只需要配置下环境变量就可以了. 具体来讲,就是将python的安装目录加入到系统的path中即可.
- C++ std::tr1::bind使用
1. 简述 同function函数相似.bind函数相同也能够实现相似于函数指针的功能.但却却比函数指针更加灵活.特别是函数指向类 的非静态成员函数时.std::tr1::function 能够对静态 ...
- MAC上反编译android apk---apktool, dex2jar, jd-jui安装使用(含手动签名)
前文 介绍了在Windows平台利用强大的APK-Multi-Tool进行反编译apk,修改smali源码后再回编译成apk的流程,最近受人之托,破解个apk,所幸的是所用到的这三个软件都是跨平台的, ...
- vi和vim上查找字符串
方法/步骤 1 我们以samba的配置文件为例,搜索一个user的字符串. vim /etc/samba/smb.conf 打开smb.conf 2 命令模式下,输入/user "/&quo ...
- discuz的php7版本号
php7的安装 wget http://am1.php.net/get/php-7.0.4.tar.gz/from/this/mirror tar zvxf php-7.0.4.tar.gz cd p ...