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)
作者:奋斗的珞珞
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.