1、安装pymemcache
pip install pymemcache
2、Memcached的使用
相关文档:https://pymemcache.readthedocs.io/en/latest/
1)创建Client连接对象
ipv4_client = Client('127.0.0.1')
ipv4_client_with_port = Client('127.0.0.1:11211')
ipv4_client_using_tuple = Client(('127.0.0.1', 11211))
ipv6_client = Client('[::1]')
ipv6_client_with_port = Client('[::1]:11211')
ipv6_client_using_tuple = Client(('::1', 11211))
domain_client = Client('localhost')
domain_client_with_port = Client('localhost:11211')
domain_client_using_tuple = Client(('localhost', 11211))
2)简单使用代码
from pymemcache.client.base import Client
client = Client('127.0.0.1:11211')
client.set('some_key', 'some_value')
result = client.get('some_key')
client.delete('some_key'")
3)多个Memcached的使用
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')
client.delete('some_key'")
相关文档:https://github.com/pinterest/pymemcache
3、python-memcached和pylibmc
相关文档:
https://github.com/linsomniac/python-memcached/issues/177
https://github.com/linsomniac/python-memcached/issues/95
python-memcached最近几年没有更新,pymemcache得到了大公司的支持。新的项目推荐使用pymemcache。python-memcached 库实现了整个 memcached 文本协议,所有套接字调用都有一个超时,并且具有灵活的序列化和反序列化方法。它也是完全用 Python 编写的,因此可以很好地与 gevent 等库配合使用。但是,它与使用线程局部变量有关,没有实现“noreply”,不能将错误视为缓存未命中,并且比 pylibmc 和 pymemcache 都慢。它还与处理 memcached 服务器集群的特定方法相关联。
pylibmc库是 libmemcached 的包装器,用 C 实现。它速度快,实现了一致的哈希、完整的 memcached 协议和超时。它不提供对“noreply”标志的访问。它也不是纯 Python,因此将它与 gevent 之类的库一起使用是不可能的,并且它对 libmemcached 的依赖带来了挑战(例如,它必须针对将在运行时使用的相同版本的 libmemcached 构建)。
相关文档:https://github.com/lericson/pylibmc
4、使用telnet管理Memcached命令
1)stats查看状态
stats
STAT pid 13610
STAT uptime 43439
STAT time 1651478946
STAT version 1.4.36
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 8.095562
STAT rusage_system 8.052116
STAT curr_connections 5
STAT total_connections 8
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 1
STAT cmd_set 2
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 1
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 72945
STAT bytes_written 974
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT bytes 72863
STAT curr_items 2
STAT total_items 2
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
END
2)stats items查看存储数据
stats items
STAT items:4:number 1
STAT items:4:age 43530
STAT items:4:evicted 0
STAT items:4:evicted_nonzero 0
STAT items:4:evicted_time 0
STAT items:4:outofmemory 0
STAT items:4:tailrepairs 0
STAT items:4:reclaimed 0
STAT items:4:expired_unfetched 0
STAT items:4:evicted_unfetched 0
STAT items:4:crawler_reclaimed 0
STAT items:4:crawler_items_checked 0
STAT items:4:lrutail_reflocked 0
STAT items:31:number 1
STAT items:31:age 43530
STAT items:31:evicted 0
STAT items:31:evicted_nonzero 0
STAT items:31:evicted_time 0
STAT items:31:outofmemory 0
STAT items:31:tailrepairs 0
STAT items:31:reclaimed 0
STAT items:31:expired_unfetched 0
STAT items:31:evicted_unfetched 0
STAT items:31:crawler_reclaimed 0
STAT items:31:crawler_items_checked 0
STAT items:31:lrutail_reflocked 0
END
3)查看详细数据
stats cachedump slab_id limit_num
slab_id
为stats items
返回的结果(STAT items后面的数字)决定的。limit_num
是返回数据的数量,0
为显示所有数据。
stats cachedump 31 0
ITEM :1:views.decorators.cache.cache_page..GET.b2b1f6168313465a6201b715caa5a9e2.d41d8cd98f00b204e9800998ecf8427e.zh-Hans [72519 b; 1651436119 s]
END
4)get
get 命令获取存储在 key(键) 中的 value(数据值) ,如果 key 不存在,则返回空。如下,
get key
或
get key1 key2 key3
5)gets
gets 命令获取带有 CAS 令牌存 的 value(数据值) ,如果 key 不存在,则返回空。如下,
gets key
或
gets key1 key2 key3
6)delete
delete 命令用于删除已存在的 key(键)。如下,
delete key [noreply]
7)incr 与 decr 命令用于对已存在的 key(键) 的数字值进行自增或自减操作
decr/incr key value
key:键值 key-value 结构中的 key,用于查找缓存值。
value:增加或减少的数值。