Python操作Memcache


Python 操作memcache

Python 操作memcache 的库有很多,比如pylibmc ,python-memcache,memcache_client.以及pymemcache .

  • pylibmc 是在c 语言实现的,并不是纯python 实现,它和协程的工作有一定的阻碍.
  • python-memcache :纯pthon 实现,但是不支持noreply ,比其他库要慢.
  • memcache-client:纯python 实现,但是不支持大多数memcache API.
  • pymemcache :纯python 实现,支持noreply .

基于简单高效的原则,这是使用pymemcache .

1.安装

$ pip install pymemcache

官方文档

2.基本使用

导入客户端类,并传递数据.

>>> from pymemcache.client.base import Client
>>> help(Client)
class Client(builtins.object)
Client(server, serializer=None, deserializer=None, connect_timeout=None, timeout=None, no_delay=False, ignore_exc=False, socket_module=socket, key_prefix=b'', default_noreply=True, allow_unicode_keys=False, encoding='ascii')

常用的参数有:

  • server:指定一个tuple 元组.包含(ip,port) .
  • allow_unicode_keys:指定一个布尔值,默认是False ,结合encoding='ascii' ,指定了编码字符串,指定为True 时,结合encoding='utf8 ,可以使用utf8 编码.
  • ignore_exc :默认是False ,True 会导致get\gets 将任何错误都视为高速缓存.
  • serializer 序列化函数,接受2个值key,value
  • deserialzer :反序列化函数,接受3个值,key,value,flag ,更多信息参照

创建为:

from pymemcache.client.base import Client

client = Client(('192.168.0.101',11211),allow_unicode_keys=True,encoding='utf8')

client 拥有memcached 的所有方法:

>>> from pymemcache.client.base import Client
>>> import pdir
>>> pdir(Client)
function:
    add: The memcached "add" command.
    append: The memcached "append" command.
    cache_memlimit: The memcached "cache_memlimit" command.
    cas: The memcached "cas" command.
    check_key: Checks key and add key_prefix.
    close: Close the connection to memcached, if it is open. The next call to a
    decr: The memcached "decr" command.
    delete: The memcached "delete" command.
    delete_many: A convenience function to delete multiple keys.
    delete_multi: A convenience function to delete multiple keys.
    flush_all: The memcached "flush_all" command.
    get: The memcached "get" command, but only for one key, as a convenience.
    get_many: The memcached "get" command.
    get_multi: The memcached "get" command.
    gets: The memcached "gets" command for one key, as a convenience.
    gets_many: The memcached "gets" command.
    incr: The memcached "incr" command.
    prepend: The memcached "prepend" command.
    quit: The memcached "quit" command.
    replace: The memcached "replace" command.
    set: The memcached "set" command.
    set_many: A convenience function for setting multiple values.
    set_multi: A convenience function for setting multiple values.
    stats: The memcached "stats" command.
    touch: The memcached "touch" command.
    version: The memcached "version" command.

3.增删改查

  • set(key, value, expire=0, noreply=None, flags=None) :Memcache set 命令

  • set_many(values, expire=0, noreply=None, flags=None) :添加多个,values 是一个字典对象,指定多个键值对.

  • set_multi()set_many()

  • add(key, value, expire=0, noreply=None, flags=None) :Memcache add 命令.

  • append( key, value, expire=0, noreply=None, flags=None):Memcache append 命令

  • prepend( key, value, expire=0, noreply=None, flags=None):Memcache prepend 命令.

  • replace(key,value,expire=0,noreply=None,flags=None) :Memcache replace 命令.

  • get(key, default=None) :Memcache get 命令,default指定没有key 返回的值.

  • get_many(keys) :返回多个值,keys 是一个list 列表类型.

  • get_multi(keys):同get_many

  • gets(key, default=None, cas_default=None) :Memcache gets命令

  • gets_many(keys) :返回多个值,keys 是一个list 列表类型.

  • cas(self, key, value, cas, expire=0, noreply=False, flags=None) :cas 只能是int\str 数据类型,并且只能是0-9 .

  • stats(*args) :查看各种状态

  • delete(key, noreply=None).

  • incr( key, value, noreply=False)

  • decr(key, value, noreply=False)

from pymemcache.client.base import Client

client = Client(('192.168.0.101',11211),allow_unicode_keys=True,encoding='utf8')

# 1.存储命令
# 增加一个
client.set('key','value')
# 增加多个
client.set_many({'测试1':'测试1','test2':'test2'},expire=120,noreply=True,flags=0)
client.set_multi({'测试2':'test2','test3':'test3'},expire=120,noreply=True,flags=0)
# add新添加
client.add('key1','value1',expire=120,noreply=True,flags=0)
# 追加
client.append('key1','append end',expire=120,noreply=True,flags=0)
client.prepend('key1','prepend start',expire=120,noreply=True,flags=0)


# 2.查找命令
result = client.get('key')
result1 = client.get_many(['测试1','test2'])
result2 = client.get_multi(['测试2','test3'])
result3 = client.get('kye1')
result4 = client.gets('key1')
# 删除
client.delete('key')
# 自增 自减
client.set('age',10,expire=120,noreply=True,flags=0)
client.incr('age',5)
client.decr('age',6)
print(client.get('age'))

# 3.清空
client.flush_all()

print(result)
print(result1)
print(result2)
print(result3)
print(result4)

4.分布式集群

使用分布式memcache

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')

5.序列化

from pymemcache.client.base import  Client
import json

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(('192.168.0.101', 11211), serializer=json_serializer,
             deserializer=json_deserializer)
client.set('key', {'a':'b', 'c':'d'})
result = client.get('key')

print(result)

输出字符串

{'a': 'b', 'c': 'd'}

6.建议

  • 构造Clinet 时,添加timeout 的配置,放置block .

  • 使用noreply 来提高性能,默认情况下,set\add\replace\append\prepend\delete 操作是开启的,而cas\incr\decr 操作是关闭的.

  • 尽可能的使用get_many\gets_many操作.

  • 使用ignore_exc 属性,保证在get 没有命中的情况下,memcache 杀死web 进程


文章作者: 文彦
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 文彦 !
评论
  目录