redis命令参考链接
https://www.redis.com.cn/commands.html
参考python代码
import threading
import time
import socket
import json
import redis
import logging
REDIS_HOST = '127.0.0.1'
REDIS_PASSWORD = ''
REDIS_PORT = 6379
REDIS_DB = 0
REDIS_ENCODING = 'utf-8'
REDIS_AUTH = ''
# 自动解码
DECODE_RESPONSES = True
MAX_CONNECTIONS = 10
class Redis_Client(object):
def __init__(self, config = None):
config = None
connection_pool = None
connectionclient = None
if not config:
config = {
'REDIS_HOST':REDIS_HOST,
'REDIS_PORT':REDIS_PORT,
'REDIS_AUTH':REDIS_PASSWORD,
'REDIS_DB':REDIS_DB,
'REDIS_ENCODING':REDIS_ENCODING,
'DECODE_RESPONSES':DECODE_RESPONSES,
'MAX_CONNECTIONS':MAX_CONNECTIONS
}
self.config = config
max_conn = 1
if 'MAX_CONNECTIONS' in self.config:
max_conn = self.config['MAX_CONNECTIONS']
if max_conn <= 0:
max_conn = 1
decode_responses = False
if 'DECODE_RESPONSES' in config:
decode_responses = config['DECODE_RESPONSES']
temp_pool = redis.ConnectionPool(host = self.config['REDIS_HOST'], port=self.config['REDIS_PORT'], db = self.config['REDIS_DB'],
password=self.config['REDIS_AUTH'], encoding=self.config['REDIS_ENCODING'], max_connections=max_conn,
decode_responses=decode_responses
)
self.connection_pool = temp_pool
temp_client = redis.Redis(connection_pool=self.connection_pool)
self.connection_client = temp_client
# 右插入元素
def rpush(self, key, json_text, expired_in_seconds = 0):
r = self.connection_client
pipe = r.pipeline()
pipe.rpush(key, json_text)
if expired_in_seconds > 0:
pipe.expire(key, expired_in_seconds)
pipe.execute()
# redis = Redis_Client()
# redis.connection_client.set('name', 'xiaohei')
# 左插入元素
def lpush(self, key, json_text, expired_in_seconds = 0):
r = self.connection_client
pipe = r.pipeline()
pipe.lpush(key, json_text)
if expired_in_seconds > 0:
pipe.expire(key, expired_in_secondes)
pipe.execute()
# 删除列表元素
def lrem(self, key, value, num = 0):
'''
key: key
value:要删除的值
num:
num=0 删除列表中所有的指定值
num=2 从前到后,删除2个
num=-2 从后到钱,删除2个
'''
r = self.connection_client
pipe = r.pipeline()
pipe.lrem(key, num, value)
pipe.execute()
# 从列表中取出最后一个元素,并插入到另外一个列表的头部; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
def brpoplpush(self, skey, mkey, timeout=0):
r = self.connection_client
pipe = r.pipeline()
pipe.brpoplpush(skey, mkey, timeout)
pipe.execute()
# pipeline左插入元素
def lpush_pipline(self, key, list_json, expired_in_seconds = 0):
r = self.connection_client
with r.pipeline(transaction=False) as pipe:
pipe.lpush(key, *[x for x in list_json])
if expired_in_seconds > 0:
pipe.expire(key, expired_in_seconds)
pipe.execute()
# pipeline左删除元素
def lpop_pipeline(self, key, length):
# 删除列表中指定个数
i = 0
poped_items = []
r = self.connection_client
current_len = r.llen(key)
if current_len > 0:
target_len = 0
if current_len > length:
target_len = length
else:
target_len = current_len
pipe = r.pipeline()
while i < target_len:
pipe.lpop(key)
i += 1
temp_poped_items = pipe.execute()
poped_items = temp_poped_items
return poped_items
# 左删除
def lpop(self, key):
poped_items = []
r = self.connection_client
data = r.lpop(key)
if data:
poped_items.append(data)
return poped_items
# pipeline右删除元素
def rpop_pipline(self, key, length):
i = 0
poped_items = []
r = self.connection_client
current_len = r.llen(key)
if current_len > 0:
target_len = 0
if current_len > length:
target_len = length
else:
target_len = current_len
pipe = r.pipeline()
while i < target_len:
pipe.rpop(key)
i += 1
temp_poped_items = pipe.execute()
poped_items = temp_poped_items
return poped_items
# 右删除元素
def rpop(self, key):
poped_items = []
r = self.connection_client
data = r.rpop(key)
if data:
poped_items.append(data)
return poped_items
def hincrby(self, hash_key, field, amount):
r = self.connection_client
result = r.hincrby(hash_key, field, amount)
return result
def llen(self, key):
r = self.connection_client
result = r.llen(key)
return result
# 删除key
def hdel(self, key, field):
r = self.connection_client
result = r.hdel(key, field)
return result
# 哈希表赋值
def hset(self, key, field, value, expired_in_seconds = 0):
r = self.connection_client
pipline = r.pipeline()
pipline.hset(key, field, value)
if expired_in_seconds > 0:
pipline.expire(key, expired_in_seconds)
pipline.execute()
# 打印信息
def info(self, section = None):
r = self.connection_client
result = r.info(section)
return result
def __max_memory_distance(self, redis_info_dict, target_max):
# # Memory
# used_memory:472978192
# used_memory_human:451.07M
# used_memory_rss:510640128
# used_memory_peak:493548568
# used_memory_peak_human:470.68M
# used_memory_lua:35840
# mem_fragmentation_ratio:1.08
# mem_allocator:jemalloc - 3.6.0
result = None
if "used_memory" in redis_info_dict.keys():
temp_used = int(redis_info_dict["used_memory"])
temp_used = temp_used / (1024 * 1024)
result = target_max - temp_used
else:
logging.warning(u"used_memory is not found!")
return result
def exceed_memory_limits(self):
result = False
if "target_max_memory" in self.config.keys():
target_max_memory = self.config["target_max_memory"]
redis_info = self.info("memory")
distance = self.__max_memory_distance(redis_info, target_max_memory)
if distance and distance <= 0:
result = True
return result
# 集合添加
def sadd(self, key, value):
r = self.connection_client
result = r.sadd(key, value)
return result
# 集合中是否有该元素
def sismember(self, key, value):
r = self.connection_client
result = r.sismember(key, value)
return result
# 是否存在key
def exists(self, key):
r = self.connection_client
result = r.exists(key)
return result
# 匹配key
def keys(self, pattern):
r = self.connection_client
result = r.keys(pattern = pattern)
return result
# 删除key
def delete(self, key):
r = self.connection_client
r.delete(key)
# 扫描
def scan(self, cursor, match = None, count = 50):
r = self.connection_client
result = r.scan(cursor = cursor, match = match, count = count)
return result
# 获取属性值
def hmget(self, hash_key, fields_list):
r = self.connection_client
result = r.hmget(hash_key, fields_list)
return result
# 设置字符串的值
def set(self, key, value, ex = None):
r = self.connection_client
result = r.set(key, value, ex)
return result
def close(self):
if self.connection_pool:
self.connection_pool.disconnect()