zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    [C++]仿java.lang.String的字符串工具类[原]
    SQL基础1创建表、用户
    Linux中gdb 查看core堆栈信息
    Direct3D9基础工具类[原]
    eclipse3.4启动错误
    ndk连接第三方库
    数据库基本概念
    MySQL常见命令
    MySQL启动和停止
    MySQL配置文件
  • 原文地址:https://www.cnblogs.com/dmir/p/5162475.html
Copyright © 2011-2022 走看看