操作模式
r = redis.Redis(host="192.168.80.20", port=6379) r.set("name", "jiachen") print(r.get("name"))
连接池
pool = redis.ConnectionPool(host="192.168.80.20", port=6379) r = redis.Redis(connection_pool=pool) r.set("name", "Jiachen") print(r.get("name"))
- string操作
- hash操作
- list操作
- set操作
- 其他操作
http://www.redis.cn/commands.html
管道
import redis import time pool = redis.ConnectionPool(host=‘192.168.80.20‘, port=6379) r = redis.Redis(connection_pool=pool) # pipe = r.pipeline(transaction=False) pipe = r.pipeline(transaction=True) pipe.set(‘name‘, ‘wupeiqi‘) time.sleep(50) pipe.set(‘role‘, ‘sb‘) pipe.execute()
订阅/发布
import redisclass RedisHelper: def __init__(self): self.__conn = redis.Redis(host=‘10.211.55.4‘) self.chan_sub = ‘fm104.5‘ self.chan_pub = ‘fm104.5‘ def public(self, msg): self.__conn.publish(self.chan_pub, msg) return True def subscribe(self): pub = self.__conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub
订阅
from monitor.RedisHelper import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe() while True: msg= redis_sub.parse_response() print msg
发布
from monitor.RedisHelper import RedisHelper obj = RedisHelper() obj.public(‘hello‘)
时间: 2024-10-14 10:14:19