首页 > 数据库技术 > 详细

python-从redis数据库中读数据

时间:2016-01-27 12:14:09      阅读:282      评论:0      收藏:0      [点我收藏+]
  • 读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)

python-从redis数据库中读数据

原文:http://www.cnblogs.com/dmir/p/5162475.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!