[Python-memcached]Python操作memcached

安装python-memchached插件

pip install python-memcached

Collecting python-memcached
  Downloading python_memcached-1.59-py2.py3-none-any.whl (16 kB)
Requirement already satisfied: six>=1.4.0 in /Users/shilei/.pyenv/versions/3.7.4/envs/flask/lib/python3.7/site-packages (from python-memcached) (1.14.0)
Installing collected packages: python-memcached
Successfully installed python-memcached-1.59

 实例代码

#!/usr/bin/env pyhton
# coding:utf-8
# @Time     : 2020-02-16 13:25
# @Author   : LeoShi
# @Site     : 
# @File     : memcached_demo.py
# @Software : MacOS Python 3.7

import memcache

# 建立连接
memcache_client = memcache.Client(['127.0.0.1:11211','172.16.1.1:11211'],debug=True)

# 添加数据
memcache_client.set('username','hello word',time=60)
memcache_client.set_multi({'email':'snoobaby@163.com','telephone':'13333333333'},time=60)

# 获取数据
# 13333333333
print(memcache_client.get('telephone'))

# 删除数据
# None
memcache_client.delete('email')
print(memcache_client.get('email'))

# 自增长
# 28
memcache_client.set('age',18,time=60)
for _ in range(10):
    memcache_client.incr('age')
print(memcache_client.get('age'))

# 自减少
# 8
memcache_client.set('age',18,time=60)
for _ in range(10):
    memcache_client.decr('age')
print(memcache_client.get('age'))

 

posted @ 2020-02-16 13:44  LeoShi2020  阅读(400)  评论(0编辑  收藏  举报