python简单操作redis
1.安装redis
命令:sudo pip install redis
2.连接redis
import redis redis_client = redis.Redis("192.168.86.127", port=6379, db=10)
3.在indexs对应的list中添加10条数据
for i in range(10): redis_client.rpush("indexs",i)
4.查看indexs键中长度
llen = redis_client.llen("indexs") print 'llen:',llen
5.从redis某一键中移除一条数据
job = redis_client.lpop("indexs") print 'job:',job
6.获取indexs键中所有数据
jobs = redis_client.lrange('indexs',0,-1)
7.redis设置值
redis_client.hmset("hot_keys",{"singers":"hot_singer"}) redis_client.hmset("hot_keys",{"songs":"hot_song"})
8.redis获取值
arraykey=["singers","songs"] for key in arraykey: for line in redis_client.hmget("hot_keys",key): print key,line
9.redis获取某一db下所有keys
print redis_client.keys()
10.redis删除某一key
redis_client.delete('indexs')