redis与python交互

about-BY-gentle.jpg

#sudo pip3 install redis

import redis
import time
#创建链接
#host='localhost', port=6379, db=0, password=None, 
redis_conn = redis.StrictRedis(host='localhost',port=6379,db=2)
#redis_conn = redis.Redis(host='localhost',port=6379)

#字符串的操作
def string():
    #redis_conn.set('classs1804',22)
    #redis_conn.mset({'1804':11,'1806':111})
    #result = redis_conn.get(['1804','1806'])
    #print(result)
    #redis_conn.setex('1804',30,'new')
    #result = redis_conn.append('1806','rr')
    #print(result)
    #result = redis_conn.strlen('1806')
    #print(result)
    #删除,返回0||1
    result = redis_conn.delete('1806')
    print(result)
def k():
    #获取数据库中所以有符合条件的键
    keys= redis_conn.keys('*z')
    print(keys)
    #判断某个键是否存在
    result = redis_conn.exists('zzz')
    print(result)
    #判断键对应值的类型
    result = redis_conn.type('zzz')
    print(result)
    #设置过期时间
    result = redis_conn.expire('zzz',30)
    print(result)
    #查看过期时间
    result = redis_conn.ttl('zzz')
    print(result)
    #返回指定范围的值
    result = redis_conn.getrange('class1804',0,-1)
    print(result)


# hash
# hset、hmset、hkeys、hget、hmget、hvals、hdel

def do_hset():
    #redis_conn.hset('info','name','zwq')
    #result = redis_conn.hget('info','name')
    #print(result)
    
    #
    # redis_conn.hmset('info1',{'name':'zz','age':'xx'})

    #result = redis_conn.hmget('info1','name','age')
    #print(result)

    #result = redis_conn.hkeys('info1')
    #print(result)
    #result = redis_conn.hvals('info1')
    #print(result)
    redis_conn.hdel('info1','name')


# list
# lpush、rpush、linsert、lrange、lset、lrem

def do_list():
    #redis_conn.lpush('l1',1,2,3,4,5)

    #result = redis_conn.lrange('l1',0,-1)
    #print(result)
    #redis_conn.rpush('l2',1,2,3,4,5)
    
    #redis_conn.linsert('l1','after',5,6)

    #redis_conn.lset('l2',1,66)

    redis_conn.lrem('l2',1,66)




#set
# # sadd、smembers、srem

def do_set():
    redis_conn.sadd('s1',1,2,3)

    result = redis_conn.smembers('s1')
    print(result)
    redis_conn.srem('s1',1)
#zset
# zadd、zrange、zrangebyscore、zscore
# 、zrem、zremrangebyscore

def do_zset():
    redis_conn.zadd('z1',1,1,1,2)
    result = redis_conn.zrange('z1',0,-1)
    print(result)
    result = redis_conn.zscore('z1',1)
    print(result)
    result = redis_conn.zrangebyscore('z1',0,1)
    print(result)
    result = redis_conn.zremrangebyscore('z1',0,1)
    print(result)
    redis_conn.zrem('z1',1,2)

def pipeline():
    #实例方法,返回一个管道对象
    pipe = redis_conn.pipeline()
    #开启事物
    pipe.multi()
    #存值
    pipe.set('age',10)
    pipe.lpush('list',[1,2,3])
    pipe.hset('students','name','vvvv')
    time.sleep(30)
    pipe.execute()

#订阅发布
def do_subscribe():
    pubsub = redis_conn.pubsub()
    #订阅频道
    pubsub.subscribe('1804')
    #解析响应结果
    #[b'subscribe', b'1804', 1]
    print(pubsub.parse_response())
    print('waiting')
    #不停监听消息
    return pubsub.parse_response()

        

#发布端
def do_publish():
    #发布频道消息
    redis_conn.publish('1804','this a dog')





if __name__ == '__main__':
    #string()
    #k()
    #pipeline()
    #do_subscribe()
    #do_publish()
    #while True:
    #    print(pubsub.parse_response())s
    #do_hset()
    #do_list()
    #do_set()
    do_zset()






你可能感兴趣的:(redis与python交互)