2025-04-21 10:04:40 +08:00
|
|
|
import redis
|
|
|
|
|
|
2025-12-30 16:49:08 +08:00
|
|
|
from app.core.config import settings
|
2025-04-21 10:04:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Redis(object):
|
|
|
|
|
"""
|
|
|
|
|
redis数据库操作
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _get_r():
|
2025-12-30 16:49:08 +08:00
|
|
|
host = settings.REDIS_HOST
|
|
|
|
|
port = settings.REDIS_PORT
|
2025-04-21 10:04:40 +08:00
|
|
|
db = 0
|
|
|
|
|
r = redis.StrictRedis(host, port, db)
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def write(cls, key, value, expire=None):
|
|
|
|
|
"""
|
|
|
|
|
写入键值对
|
|
|
|
|
"""
|
|
|
|
|
# 判断是否有过期时间,没有就设置默认值
|
|
|
|
|
if expire:
|
|
|
|
|
expire_in_seconds = expire
|
|
|
|
|
else:
|
|
|
|
|
expire_in_seconds = 100
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
r.set(key, value, ex=expire_in_seconds)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def read(cls, key):
|
|
|
|
|
"""
|
|
|
|
|
读取键值对内容
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
value = r.get(key)
|
|
|
|
|
return value.decode('utf-8') if value else value
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def hset(cls, name, key, value):
|
|
|
|
|
"""
|
|
|
|
|
写入hash表
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
r.hset(name, key, value)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def hget(cls, name, key):
|
|
|
|
|
"""
|
|
|
|
|
读取指定hash表的键值
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
value = r.hget(name, key)
|
|
|
|
|
return value.decode('utf-8') if value else value
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def hgetall(cls, name):
|
|
|
|
|
"""
|
|
|
|
|
获取指定hash表所有的值
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
return r.hgetall(name)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, *names):
|
|
|
|
|
"""
|
|
|
|
|
删除一个或者多个
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
r.delete(*names)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def hdel(cls, name, key):
|
|
|
|
|
"""
|
|
|
|
|
删除指定hash表的键值
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
r.hdel(name, key)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def expire(cls, name, expire=None):
|
|
|
|
|
"""
|
|
|
|
|
设置过期时间
|
|
|
|
|
"""
|
|
|
|
|
if expire:
|
|
|
|
|
expire_in_seconds = expire
|
|
|
|
|
else:
|
|
|
|
|
expire_in_seconds = 100
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
r.expire(name, expire_in_seconds)
|
|
|
|
|
|
2026-01-12 11:51:37 +08:00
|
|
|
@classmethod
|
|
|
|
|
def scan_keys(cls, pattern="*"):
|
|
|
|
|
"""
|
|
|
|
|
扫描匹配模式的key
|
|
|
|
|
"""
|
|
|
|
|
r = cls._get_r()
|
|
|
|
|
keys = []
|
|
|
|
|
cursor = 0
|
|
|
|
|
while True:
|
|
|
|
|
cursor, partial_keys = r.scan(cursor, match=pattern, count=1000)
|
|
|
|
|
keys.extend(partial_keys)
|
|
|
|
|
if cursor == 0:
|
|
|
|
|
break
|
|
|
|
|
return [key.decode('utf-8') if isinstance(key, bytes) else key for key in keys]
|
|
|
|
|
|
2025-04-21 10:04:40 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
redis_client = Redis()
|
|
|
|
|
# print(redis_client.write(key="1230", value=0))
|
|
|
|
|
redis_client.write(key="1230", value=10)
|
|
|
|
|
# print(redis_client.read(key="1230"))
|