1. 程式人生 > >Redis資料庫操作

Redis資料庫操作

import redis

pool=redis.ConnectionPool(host='127.0.0.1',port=6379,decode_responses=True)
r=redis.Redis(connection_pool=pool)

# set測試
# r.sadd('test_set',10,20,30)
print(r.scard('test_set'))
print(r.smembers('test_set'))

# hash測試
print(r.hgetall('test_hash'))
print(r.hkeys('test_hash'))
print(r.hvals('test_hash'))
a=dict(zip(r.hkeys('test_hash'),r.hvals('test_hash')))
print(a)
if a==r.hgetall('test_hash'):
    print('pass')
else:
    print('sorry')

# string測試
print(r.get('test_string'))

# list測試
print(r.lrange('test_list',0,-1))

# 有序set測試
print(r.zrange('test_zset',0,-1))# 輸出value
print(r.zrange('test_zset',0,-1,withscores=1))#輸出value和score
print(sorted(r.zrange('test_zset',0,-1,withscores=0)))#輸出value

for i in r.zrange('test_zset',0,-1):
    # print(r.zscore('test_zset',i))
    print([str(r.zscore('test_zset',i)),int(r.zscore('test_zset',i))][int(r.zscore('test_zset',i))==r.zscore('test_zset',i)])

# 獲取所有資料表名稱
print(r.keys())

# 判斷資料表型別
for i in r.keys():
    print(r.type(i))