当前位置:网站首页>Redis-哈希
Redis-哈希
2022-07-31 05:16:00 【Young_win】
简介
REmote DIctionary Server(Redis)是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。Redis运行在内存中 但是可以持久化到磁盘,所以在对不同数据集进行高速读写时 需要权衡内存,因为数据量不能大于硬件内存。
Redis的数据结构类型(Redis值value的类型)有5种:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
字符串的一个键 最大能存储 512MB。
每个 hash 可以存储 2^32 -1 键值对(40多亿)。
列表最多可存储 2^32 - 1 元素 (4294967295, 每个列表可存储40多亿)。
集合最大的成员数为 2^32 - 1(4294967295, 每个集合可存储40多亿个成员)。
http://redisdoc.com/index.html
redis-cli -h host -p port -a password
Python包
pip install redis-py-cluster==2.1.0
#!/bin/python3
# -*- coding:utf-8 -*-
import os, sys
import json
import copy
from rediscluster import RedisCluster
class RedisClient:
def __init__(self, config):
self._config = copy.deepcopy(config)
self._startup_nodes_list = self._config.nodes_list
self._password = self._config.redis_password
self.redis = RedisCluster(startup_nodes=self._startup_nodes_list,
password=self._password, decode_responses=True, socket_timeout=5)
def hsetnx(self, key, field, value, timeout=None): # set_new_field field已经存在,该操作无效
self.redis.hsetnx(key, field, value)
if isinstance(timeout, int):
self.redis.expire(key, timeout)
def hset(self, key, field=None, value=None, mapping=None, timeout=None): # field已经存在于哈希表中,旧值将被覆盖
if not field is None:
return self.redis.hset(key, field, value) # 一个key的单个filed:value添加
elif not mapping is None:
print('=====mapping=====', field, value, mapping)
return self.redis.hset(key, None, None, mapping) # 一个key的多个{fields:values}添加
if isinstance(timeout, int):
self.redis.expire(key, timeout)
def expire(self, key, timeout): # 对key执行命令的超时限制判断
if not isinstance(timeout, int):
raise ValueError("timeout must be integer")
self.redis.expire(key, timeout)
def hget(self, key, field):
value = self.redis.hget(key, field)
return "" if value is None else str(value)
def hgetall(self, key):
value = self.redis.hgetall(key)
return "" if value is None else str(value)
def hexists(self, key, field): # 判断key的指定field是否存在
return self.redis.hexists(key, field)
def hdel(self, key, field): # 删除key的指定field [单个field]
if self.redis.hexists(key, field):
return self.redis.hdel(key, field)
else: pass
#def hdel(self, key, fields): # 删除key的指定field key(string), fields=(filed1name,field2name,field3name, ...)
# return self.redis.hdel(key, *fields)
def close(self):
self.redis.close()
class config:
def __init__(self, _nodes_list, redis_password):
self.nodes_list = _nodes_list
self.redis_password = redis_password
if __name__=="__main__":
conf = config([{
'host':'redis.db.cloud', 'port':'10009'}],
'123456')
client = RedisClient(conf)
user_table = 'user_feature1_'
item_table = 'item_feature1_'
uid_key = user_table + '7db9ca895802365'
uid_field = 'user_behavior_timezone'
print('the uid_key is : %s, uid_field is : %s' % (uid_key, uid_field))
print('test_hgetall_ing......')
print(client.hgetall(uid_key))
print('\n')
print('test_hexists_ing......')
print(client.hexists(uid_key, uid_field))
print('test_hget_ing......')
print(client.hget(uid_key, uid_field))
print('\n')
print('test_hset_ing......')
print(client.hset(uid_key, uid_field, 'N8'), 'after_change_field: ', client.hget(uid_key, uid_field))
print(client.hset(uid_key, None, None, mapping={
uid_field: '8'}), 'recover_field: ', client.hget(uid_key, uid_field))
print('\n')
print('test_hdel_ing......')
print(client.hset(uid_key, uid_field, '8'), '%s is exists: ' % uid_field, client.hexists(uid_key, uid_field), client.hdel(uid_key, uid_field))
print('after hdel %s is exist: '% uid_field, client.hexists(uid_key, uid_field), client.hset(uid_key, uid_field, '8'))
client.close()
边栏推荐
猜你喜欢
Principle analysis of famous website msdn.itellyou.cn
Hyper-V新建虚拟机注意事项
Pytorch常用函数
Global scope and function scope in js
通信原理——纠错编码 | 汉明码(海明码)手算详解
变分自编码器VAE实现MNIST数据集生成by Pytorch
Numpy常用函数
RuntimeError: CUDA error: no kernel image is available for execution on the device问题记录
flutter arr dependencies
2022年SQL大厂高频实战面试题(详细解析)
随机推荐
Android软件安全与逆向分析阅读笔记
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
多元线性回归方程原理及其推导
js中的对象与函数的理解
mysql 事务原理详解
CNN的一点理解
Attention based ASR(LAS)
用pytorch里的children方法自定义网络
Pytorch学习笔记13——Basic_RNN
Understanding of objects and functions in js
quick-3.5 无法正常显示有混合纹理的csb文件
CMOS管原理,及其在推挽电路中的应用
Pure shell implementation of text replacement
360 加固 file path not exists.
Podspec automatic upgrade script
SSH自动重连脚本
Tencent Cloud Lightweight Server deletes all firewall rules
ROS 之订阅多个topic时间同步问题
pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用
js中流程控制语句