• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python-redis连接池

武飞扬头像
dreams_dream
帮助5

python连接redis

在python中,要操作redis,目前主要是通过一个python-redis模块来实现

1、在python中安装redis模块

pip3 install redis

2、在python中使用redis

  跟其他模块一样,在安装好redis模块后,要使用redis模块就要先导入。

python连接redis数据库: 

  1.  
    #!/usr/bin/env python
  2.  
    # -*- coding:utf8 -*-
  3.  
     
  4.  
    import redis
  5.  
     
  6.  
    '''
  7.  
    这种连接是连接一次就断了,耗资源.端口默认6379,就不用写
  8.  
    r = redis.Redis(host='127.0.0.1',port=6379,password='tianxuroot')
  9.  
    r.set('name','root')
  10.  
    print(r.get('name').decode('utf8'))
  11.  
    '''
  12.  
    '''
  13.  
    连接池:
  14.  
    当程序创建数据源实例时,系统会一次性创建多个数据库连接,并把这些数据库连接保存在连接池中,当程序需要进行数据库访问时,
  15.  
    无需重新新建数据库连接,而是从连接池中取出一个空闲的数据库连接
  16.  
    '''
  17.  
    pool = redis.ConnectionPool(host='127.0.0.1',password='helloworld') #实现一个连接池
  18.  
     
  19.  
    r = redis.Redis(connection_pool=pool)
  20.  
    r.set('foo','bar')
  21.  
    print(r.get('foo').decode('utf8'))
学新通

 封装使用

  1.  
    import redis
  2.  
     
  3.  
     
  4.  
    class Mredis:
  5.  
    """redis连接池"""
  6.  
     
  7.  
    def __init__(self) -> None:
  8.  
    self.pool = redis.ConnectionPool(host='localhost', port=6379, password=123456)
  9.  
    self.conn = redis.Redis(connection_pool=self.pool)
  10.  
     
  11.  
    # 删除键
  12.  
    def del_key(self, key):
  13.  
    self.conn.delete(key)
  14.  
     
  15.  
    # 字符串添加
  16.  
    def str_set(self, key, val):
  17.  
    self.conn.set(key, val)
  18.  
     
  19.  
    # 字条串读取
  20.  
    def str_get(self, key):
  21.  
    return self.conn.get(key)
  22.  
     
  23.  
    # 字符串添加
  24.  
    # 设置键值: name="alice" 且超时时间为10秒,(值写入到redis时会自动转字符串)
  25.  
    # conn.set("name", "alice", ex=10)
  26.  
    def str_time_set(self, key, val, time):
  27.  
    self.conn.set(key, val, ex=time)
  28.  
     
  29.  
    # 列表
  30.  
    # 从类别左侧进
  31.  
    def l_push(self, key, value):
  32.  
    self.conn.lpush(key, value)
  33.  
     
  34.  
    # 从列表右侧进
  35.  
    def r_push(self, key, value):
  36.  
    self.conn.rpush(key, value)
  37.  
     
  38.  
    # 列表长度
  39.  
    def t_len(self, key):
  40.  
    return self.conn.llen(key)
  41.  
     
  42.  
    # 从右侧移除一个元素并返回对应值
  43.  
    def r_pop(self, key):
  44.  
    return self.conn.rpop(key)
  45.  
     
  46.  
    # 获取列表中所有值
  47.  
    def all_list(self, key):
  48.  
    return self.conn.lrange(key, 0, -1)
  49.  
     
  50.  
    # 有侧开始删除n个值
  51.  
    def rem_n_value(self, key, count, value):
  52.  
    return self.conn.lrem(key, -count, value)
  53.  
     
  54.  
    # hash
  55.  
    # 添加
  56.  
    def hash_add(self, pkey, key, v):
  57.  
    self.conn.hset(pkey, key, v)
  58.  
     
  59.  
    # 单个
  60.  
    def hash_get(self, pkey, key):
  61.  
    return self.conn.hget(pkey, key)
  62.  
     
  63.  
    # 获取所有
  64.  
    def hash_getall(self, pkey):
  65.  
    return self.conn.hgetall(pkey)
  66.  
     
  67.  
    # hsetnx 给哈希表key添加field-value对,当且仅当域field不存在
  68.  
    def hash_setnx(self, pkey, key, v):
  69.  
    self.conn.hsetnx(pkey, key, v)
  70.  
     
  71.  
    # jiajian 为哈希表key中的域field的值加上<incerment>
  72.  
    def hash_jiajian(self, pkey, key, count):
  73.  
    self.conn.hincrby(pkey, key, count)
  74.  
    # 删除hash中的指定字段, 字段对应的值会一起删除
  75.  
     
  76.  
    def hsah_hdel(self, pkey, key):
  77.  
    self.conn.hdel(pkey, key)
  78.  
     
  79.  
    # setnx
  80.  
    def str_setnx(self, key, v):
  81.  
    return self.conn.setnx(key, v)
  82.  
     
  83.  
    def store_change(self, key, count, type):
  84.  
    # type1加 2减
  85.  
    if type == 1:
  86.  
    self.conn.decrby(key, count)
  87.  
    else:
  88.  
    self.conn.incrby(key, count)
  89.  
     
  90.  
    # set
  91.  
    def set_add(self, k, v):
  92.  
    self.conn.sadd(k, v)
  93.  
     
  94.  
    def set_getall(self, k):
  95.  
    return self.conn.smembers(k)
  96.  
     
  97.  
    def set_del(self, k):
  98.  
    return self.conn.delete(k)
  99.  
     
  100.  
     
  101.  
    mredis = Mredis()
学新通

更详细的请参考:

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhjaej
系列文章
更多 icon
同类精品
更多 icon
继续加载