当前位置:网站首页>Redis-Hash
Redis-Hash
2022-07-31 06:14:00 【Young_win】
简介
REmote DIctionary Server(Redis)是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API.Redis运行在内存中 But can persist to disk,所以在对不同数据集进行高速读写时 需要权衡内存,因为数据量不能大于硬件内存.
Redis的数据结构类型(Redis值value的类型)有5种:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合).
a key of the string 最大能存储 512MB.
每个 hash 可以存储 2^32 -1 键值对(40多亿).
列表最多可存储 2^32 - 1 元素 (4294967295, 每个列表可存储40多亿).
The maximum number of members in a set is 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): # 对keyExecute command timeout limit judgment
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()
边栏推荐
- Why does read in bash need to cooperate with while to read the contents of /dev/stdin
- Embedding cutting-edge understanding
- Understanding of js arrays
- DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
- sql add default constraint
- The browser looks for events bound or listened to by js
- DSPE-PEG-Biotin,CAS:385437-57-0,磷脂-聚乙二醇-生物素可延长循环半衰期
- VS connects to MYSQL through ODBC (2)
- cocos2d-x-3.2 create project method
- VS2017 connects to MYSQL
猜你喜欢

CLS-PEG-FITC Fluorescein-PEG-CLS 胆固醇-聚乙二醇-荧光素简介

Phospholipids-Polyethylene Glycol-Active Esters for Scientific Research DSPE-PEG-NHS CAS: 1445723-73-8

MySQL 主从切换步骤

Pytorch学习笔记7——处理多维特征的输入

mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体

MySQL 入门:Case 语句很好用

OpenCV中的图像数据格式CV_8U定义

Tencent Cloud GPU Desktop Server Driver Installation

qt:cannot open C:\Users\某某某\AppData\Local\Temp\main.obj.15576.16.jom for write

Sqlite column A data is copied to column B
随机推荐
2021年软件测试面试题大全
Attention based ASR(LAS)
Shell/Vim相关list
unicloud 发布后小程序提示连接本地调试服务失败,请检查客户端是否和主机在同一局域网下
Understanding of objects and functions in js
jenkins +miniprogram-ci upload WeChat applet with one click
VS2017连接MYSQL
Redis-哈希
MySQL 主从切换步骤
The browser looks for events bound or listened to by js
DSPE-PEG-COOH CAS:1403744-37-5 磷脂-聚乙二醇-羧基脂质PEG共轭物
多元线性回归方程原理及其推导
Embedding cutting-edge understanding
VTK:Could not locate vtkTextRenderer object.
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
TransactionTemplate 事务编程式写法
Understanding of js arrays
UiBot has an open Microsoft Edge browser and cannot perform the installation
MYSQL事务与锁问题处理
pytorch学习笔记10——卷积神经网络详解及mnist数据集多分类任务应用