python-从redis数据库中读数据

  • 读string,list,set,sort_set,hash类型的数据
import redis

class DataBase:
def __init__(self, host, port):
self.host = host
self.port = port def read_string(self, key):
try:
r = redis.StrictRedis(host=self.host, port=self.port)
result = r.get(key)
return result
except Exception as exception:
print(exception) def read_list(self, key, n):
try:
r = redis.StrictRedis(host=self.host, port=self.port)
if n > r.llen(key):
result = r.lrange(0, -1)
else:
result = r.lrange(key, 0, n) return result
except Exception as exception:
print(exception) def read_set(self, key):
try:
r = redis.StrictRedis(host=self.host, port=self.port)
result = r.smembers(key)
return result
except Exception as exception:
print(exception) def read_hash(self, key):
try:
r = redis.StrictRedis(host=self.host, port=self.port)
result = r.hgetall(key)
return result
except Exception as exception:
print(exception) def read_sort_set(self, key, n):
try:
r = redis.StrictRedis(host=self.host, port=self.port)
if n > r.zcard(key):
result = r.zrange(0, -1)
else:
result = r.zrange(0, n)
return result
except Exception as exception:
print(exception)
上一篇:EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子


下一篇:EF里一对一、一对多、多对多关系的配置和级联删除