当前位置: 首页 > news >正文

Python脚本之操作Redis Cluster【二】

本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045

之前写过一篇 使用redis-py来操作redis集群, https://blog.csdn.net/zyooooxie/article/details/123760358 ,这期来分享下 使用redis-py-cluster;

【实际这篇博客推迟发布N个月】

个人博客:https://blog.csdn.net/zyooooxie

【以下所有内容仅为个人项目经历,如有不同,纯属正常】

redis-py-cluster

https://pypi.org/project/redis-py-cluster/

This major version of redis-py-cluster supports redis-py >=3.0.0, <4.0.0.

代码

"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""import tracebackfrom rediscluster import ClusterConnectionPool
from rediscluster import RedisClusterfrom xxx_test.user_log import Loghost2 = ''
p1wd = ''
port = 1234
gl_key_name = 'TEST_xie*'Log.info('------')gl_real_string = ''
gl_real_hash = ''
gl_real_list = ''
gl_real_set = ''
gl_no_exist = 'TEST_zyooooxie'gl_test_str = 'test_str'
gl_test_hash = 'test_hash'
gl_test_list = 'test_list'
gl_test_set = 'test_set'Log.info('------')# pip install redis-py-cluster==2.1.3
# https://redis-py-cluster.readthedocs.io/en/2.1.3/index.htmldef redis_py_cluster_connect_1():rc = RedisCluster(startup_nodes=[{'host': host2, 'port': port}],decode_responses=True, password=pwd)Log.info('{}'.format(rc))Log.info(type(rc))Log.error('已连接')return rcdef redis_py_cluster_connect_2():ccp = ClusterConnectionPool(startup_nodes=[{'host': host2, 'port': port}],decode_responses=True, password=pwd)rc = RedisCluster(connection_pool=ccp)Log.info('{}'.format(rc))Log.info(type(rc))Log.error('已连接')return rcdef redis_py_cluster_connect_3():rc = RedisCluster(host=host2, port=port,decode_responses=True, password=pwd)Log.info('{}'.format(rc))Log.info(type(rc))Log.error('已连接')return rc
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""def cluster_commands(rc: RedisCluster):Log.info(rc.cluster_info())Log.info(rc.cluster_slots())Log.info(rc.cluster_nodes())Log.info('------')exist_key_slot = rc.cluster_keyslot(gl_real_string)  # 计算key 应该被放置在哪个slotLog.info(exist_key_slot)Log.info(rc.cluster_countkeysinslot(exist_key_slot))  # 返回  slot 目前包含的键值对数量Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 0))  # 返回 n 个 slot的键Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 1))Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 2))Log.info(rc.cluster_get_keys_in_slot(exist_key_slot, 3))Log.info('------')Log.info(rc.cluster_keyslot(gl_real_hash))Log.info(rc.cluster_keyslot(gl_real_list))Log.info(rc.cluster_keyslot(gl_real_set))Log.info(rc.cluster_keyslot(gl_no_exist))
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""def keys_commands(rc: RedisCluster):data_list = rc.keys(gl_key_name)Log.info(len(data_list))Log.info(type(data_list))Log.error('------')def scan_commands(rc: RedisCluster):data_list = list()cursor = 0# args = rc.scan(cursor, gl_key_name, count=5000)# Log.info(args)  # 返回值有问题# https://redis-py-cluster.readthedocs.io/en/2.1.3/commands.html#keys-generic# SCAN command has currently a buggy client side implementation.## It is not recommended to use any *SCAN methods.# 不建议使用任何*SCAN方法。Log.error('------')def scan_iter_method(rc: RedisCluster):args = rc.scan_iter(gl_key_name, count=5000)Log.info(args)Log.info(type(args))data = list(args)Log.info(len(data))Log.info(data[-10:])
"""
@blog: https://blog.csdn.net/zyooooxie
@qq: 153132336
@email: zyooooxie@gmail.com
"""def cluster_str(rc: RedisCluster):""":param rc::return:"""# https://redis.io/docs/data-types/strings/Log.info(rc.delete(gl_test_str))Log.info(rc.set(gl_test_str, 'https://blog.csdn.net/zyooooxie', ex=1000))Log.info(rc.get(gl_test_str))key1 = 'external:customer:xxx_1'key2 = 'external:customer:xxx_2'key3 = 'external:customer:xxx_3'key4 = 'external:TEST'Log.info(rc.mset({key1: 'value 1', key2: 'value 2', key3: '3个确定都是相同slot',key4: 'redis-py-cluster的mget、mset 支持 不同slot的key'}))Log.info(rc.mget(key1, key3, key2))Log.info(rc.mget(key1, key4))Log.info('------')Log.info('redis-py-cluster的unlink 必须是 the same slot的key')Log.info(rc.unlink(key1, key3))# Log.info(rc.unlink(key1, key4, gl_no_exist))  # Keys in request don't hash to the same slotLog.info(rc.exists(gl_test_str))Log.info(rc.type(gl_test_str))Log.info(rc.ttl(gl_test_str))Log.info(rc.expire(gl_test_str, 2 * 60 * 60))def cluster_hash(rc: RedisCluster):""":param rc::return:"""# https://redis.io/docs/data-types/hashes/Log.info(rc.delete(gl_test_hash))Log.info(rc.hset(gl_test_hash, mapping={'hash_key0': 'hash_value0', 'hash_key1': 'hash_value1','hash_key2': 'hash_value2', 'hash_key3': 'hash_value3','hk4': 'hv4', 'hk5': 'hv5','hk6': 'hv6'}))Log.info(rc.hget(gl_test_hash, 'hash_key0'))Log.info(rc.hlen(gl_test_hash))Log.info(rc.hexists(gl_test_hash, 'hash_key2222'))Log.info(rc.hkeys(gl_test_hash))Log.info(rc.hvals(gl_test_hash))Log.info(rc.hdel(gl_test_hash, 'hash_key2222', 'hash_key0', 'hk6'))Log.info(rc.hmget(gl_test_hash, 'hash_key2222', 'hash_key2'))Log.info(rc.hmget(gl_test_hash, ['hash_key2222', 'hash_key2']))Log.info(rc.hmset(gl_test_hash, {'test': 'test_value', 'test2': 'test_value2'}))Log.info(rc.hgetall(gl_test_hash))Log.info('------')Log.info(rc.hset(gl_no_exist, mapping={'test': 'test_value', 'test2': 'test_value2'}))Log.info(rc.unlink(gl_no_exist))Log.info(rc.exists(gl_test_hash))Log.info(rc.type(gl_test_hash))Log.info(rc.ttl(gl_test_hash))Log.info(rc.expire(gl_test_hash, 2 * 60 * 60))def cluster_list(rc: RedisCluster):""":param rc::return:"""# https://redis.io/docs/data-types/lists/Log.info(rc.delete(gl_test_list))Log.info(rc.rpush(gl_test_list, 'list1', 'list2', 'list3'))Log.info(rc.lindex(gl_test_list, 1))Log.info(rc.llen(gl_test_list))Log.info(rc.lpush(gl_test_list, 'list0', 'list0'))Log.info(rc.linsert(gl_test_list, 'BEFORE', 'list0', 'BEFORE__'))Log.info(rc.linsert(gl_test_list, 'AFTER', 'list0', 'AFTER__'))  # 放在第一个list0 之后Log.info(rc.lrange(gl_test_list, 0, -1))Log.info(rc.lpop(gl_test_list))Log.info(rc.rpop(gl_test_list))Log.info(rc.lrem(gl_test_list, 1, 'list0'))Log.info(rc.lset(gl_test_list, 0, '新的_0'))Log.info('------')Log.info(rc.lpush(gl_no_exist, 0, 'list_0', 1, 'list_1'))Log.info(rc.unlink(gl_no_exist))Log.info(rc.type(gl_test_list))Log.info(rc.exists(gl_test_list))Log.info(rc.ttl(gl_test_list))Log.info(rc.expire(gl_test_list, 2 * 60 * 60))def cluster_set(rc: RedisCluster):""":param rc::return:"""# https://redis.io/docs/data-types/sets/Log.info(rc.delete(gl_test_set))Log.info(rc.sadd(gl_test_set, 'set1', 'set2', 'set3', 'set3', 'set3', 'set3', 'set4'))Log.info(rc.sismember(gl_test_set, 'set1111'))Log.info(rc.srem(gl_test_set, 'set1'))Log.info(rc.scard(gl_test_set))Log.info(rc.smembers(gl_test_set))Log.info('------')Log.info(rc.sadd(gl_no_exist, 'set3', 'set3', 'set3', 'set3'))Log.info(rc.unlink(gl_no_exist))Log.info(rc.type(gl_test_set))Log.info(rc.exists(gl_test_set))Log.info(rc.ttl(gl_test_set))Log.info(rc.expire(gl_test_set, 2 * 60 * 60))if __name__ == '__main__':Log.error('------')# rc_m = redis_py_cluster_connect_1()rc_m = redis_py_cluster_connect_2()# rc_m = redis_py_cluster_connect_3()# cluster_commands(rc=rc_m)try:# cluster_str(rc_m)# cluster_hash(rc_m)# cluster_list(rc_m)# cluster_set(rc_m)Log.error(gl_key_name)scan_commands(rc_m)keys_commands(rc_m)# scan_iter_method(rc_m)except Exception as e:Log.error(e.args)Log.info(traceback.format_exc())rc_m.close()Log.error('------')

本文链接:https://blog.csdn.net/zyooooxie/article/details/112484045

个人博客 https://blog.csdn.net/zyooooxie

相关文章:

Python脚本之操作Redis Cluster【二】

本文为博主原创&#xff0c;未经授权&#xff0c;严禁转载及使用。 本文链接&#xff1a;https://blog.csdn.net/zyooooxie/article/details/112484045 之前写过一篇 使用redis-py来操作redis集群&#xff0c; https://blog.csdn.net/zyooooxie/article/details/123760358 &am…...

认识数学建模

文章目录 1 什么是数学建模2 数学建模的比赛形式3 参加数学建模的好处4 数学建模的流程5 数学建模成员分工6 数学建模常用软件7 数学建模竞赛7.1 美国大学生数学建模竞赛7.2 MathorCup高校数学建模挑战赛7.3 华中杯大学生数学建模挑战赛7.4 认证杯数学建模网络挑战赛7.5 华东杯…...

计算机工作原理解析和解剖(基础版)

我们会从软件⼯程师的⻆度解释计算机是如何⼯作的&#xff0c;我们的主要⽬标既不是期待 ⼤家可以造出⾃⼰的计算机&#xff0c;也不是介绍如何编程&#xff0c;⽽是希望让⼤家了解计算机的核⼼⼯作机制后&#xff0c;打破计算机的神秘感&#xff0c;并且有利于理解我们平时编程…...

外网ssh远程连接服务器

文章目录 外网ssh远程连接服务器一、前言二、配置流程1. 在服务器上安装[cpolar](https://www.cpolar.com/)客户端2. 查看版本号&#xff0c;有正常显示版本号即为安装成功3. token认证4. 简单穿透测试5. 向系统添加服务6. 启动cpolar服务7. 查看服务状态8. 登录后台&#xff0…...

滴滴基于 Ray 的 XGBoost 大规模分布式训练实践

背景介绍 作为机器学习模型的核心代表&#xff0c;XGBoost 在滴滴众多策略算法业务场景中发挥着至关重要的作用。因此&#xff0c;保障并持续提升 XGBoost 模型的离线训练及在线推理稳定性一直是机器学习平台的重点工作。同时&#xff0c;面对多样化的业务场景定制需求和数据规…...

k8s从入门到实践

k8s从入门到实践 介绍 Kubernetes&#xff08;简称k8s&#xff09;和Docker Swarm是两个流行的容器编排工具&#xff0c;它们都可以帮助用户管理和部署分布式应用&#xff0c;尤其是基于容器的应用。以下是两者的主要特点和对比&#xff1a; Kubernetes (k8s)&#xff1a; 开…...

Qt5.12.0 与 VS2017 在 .pro文件转.vcxproj文件

一、参考资料 stackoverflow qt - How to generate .sln/.vcproj using qmake - Stack Overflowhttps://stackoverflow.com/questions/2339832/how-to-generate-sln-vcproj-using-qmake?answertabtrending#tab-topqt - 如何使用 qmake 生成 .sln/.vcproj - IT工具网 (coder.wo…...

金蝶云星空 ServiceGateway RCE漏洞复现

0x01 产品简介 金蝶云星空是一款云端企业资源管理(ERP)软件,为企业提供财务管理、供应链管理以及业务流程管理等一体化解决方案。金蝶云星空聚焦多组织,多利润中心的大中型企业,以 “开放、标准、社交”三大特性为数字经济时代的企业提供开放的 ERP 云平台。服务涵盖:财…...

二叉树的最大深度[简单]

优质博文&#xff1a;IT-BLOG-CN 一、题目 给定一个二叉树root&#xff0c;返回其最大深度。 二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;3 示例 2&#xff1a…...

[Redis]不同系统间安装redis服务器

日常服务器端开发&#xff0c;消息队列等需求&#xff0c;免不了用到redis&#xff0c;搭建一个redis服务器&#xff0c;方便开发和测试&#xff0c;我们从以下三类系统来说明下&#xff1a; 安装 Redis 服务器的过程因操作系统而异。以下是在常见的 Linux 发行版&#xff08;…...

Unity之动画和角色控制

目录 &#x1f4d5; 一、动画 1.创建最简单的动画 2.动画控制器 &#x1f4d5;二、把动画和角色控制相结合 &#x1f4d5;三、实现实例 3.1 鼠标控制角色视角旋转 3.2 拖尾效果 &#x1f4d5;四、混合动画 最近学到动画了&#xff0c;顺便把之前创建的地形&#xff0…...

C语言库函数实现字符串转大小写

目录 引言 代码 引言 处理字符串时&#xff0c;除了将字符串中的所有大写字母转换为小写字母外&#xff0c;我们还可以利用其他相关函数进行更丰富的文本操作。本文将以一段使用isupper()、tolower()函数实现字符串全转小写的C语言程序为例&#xff0c;详细介绍这两个函数以及…...

hcip----ospf

一&#xff1a;动态路由协议 IGP 协议---RIP OSPF ISIS EIGRP EGP--EGP ---BGP 三个角度的评判一款动态路由协议的优劣 RIP --request response 1.选路--选路依据不好&#xff0c;可能出现环路 2.收敛速度--计时器 3.占用资源-- RIPV1 RIPV2 RIPNG--ipv6 OSPFV1 OSPFV…...

vue中如何写过滤器

全局注册 (可以在main.js中进行全局注册 vue.fifler(test’&#xff0c;function(v){return v0? ‘终止’&#xff1a;v1?进行中:异常 })在组件页面使用 <view>{{state|test}}</view> <script> export default {data(){return {state: 1// state 1 进行中…...

c语言-文件的读写操作(下)

文章目录 前言一、文件的随机读写1.1 fseek()1.2 ftell()1.3 rewind() 总结 前言 本篇文章介绍c语言中文件的随机读写 一、文件的随机读写 1.1 fseek() fseek()函数的作用是根据文件指针的位置和偏移量定位文件指针 int fseek ( FILE * stream, long int offset, int origi…...

android学习笔记----SQLite数据库

用SQLite语句执行&#xff1a; 首先看到界面&#xff1a; 代码如下&#xff1a; MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditTe…...

开发知识点-Flutter移动应用开发

支持 安卓 IOS Android 鸿蒙 第一章dart基础章节介绍 移动电商——Flutter-广告Banner组件制作 移动电商——Flutter实战课程介绍 Flutter实例——路由跳转的动画效果...

视频尺寸魔方:分层遮掩3D扩散模型在视频尺寸延展的应用

▐ 摘要 视频延展(Video Outpainting)是对视频的边界进行扩展的任务。与图像延展不同&#xff0c;视频延展需要考虑到填充区域的时序一致性&#xff0c;这使得问题更具挑战性。在本文中&#xff0c;我们介绍了一个新颖的基于扩散模型的视频尺寸延展方法——分层遮掩3D扩散模型(…...

openssl3.2/test/certs - 061 - other@good.org not permitted by CA1

文章目录 openssl3.2/test/certs - 061 - othergood.org not permitted by CA1概述笔记END openssl3.2/test/certs - 061 - othergood.org not permitted by CA1 概述 openssl3.2 - 官方demo学习 - test - certs 笔记 /*! * \file D:\my_dev\my_local_git_prj\study\openSS…...

如何实现无公网ip远程访问本地websocket服务端【内网穿透】

文章目录 1. Java 服务端demo环境2. 在pom文件引入第三包封装的netty框架maven坐标3. 创建服务端,以接口模式调用,方便外部调用4. 启动服务,出现以下信息表示启动成功,暴露端口默认99995. 创建隧道映射内网端口6. 查看状态->在线隧道,复制所创建隧道的公网地址加端口号7. 以…...

RabbitMQ源代码热更新技巧:version_up模块实现无停机升级

RabbitMQ源代码热更新技巧&#xff1a;version_up模块实现无停机升级 【免费下载链接】RabbitMQ RabbitMQ系统3.5.3版本中文完全注释(同时实现了RabbitMQ系统和插件源代码编译&#xff0c;根据配置文件创建RabbitMQ集群&#xff0c;创建连接RabbitMQ系统的客户端节点等相关功能…...

如何解决 gh_mirrors/pkg/pkg 与 Yarn PnP 的兼容性问题:完整测试指南

如何解决 gh_mirrors/pkg/pkg 与 Yarn PnP 的兼容性问题&#xff1a;完整测试指南 【免费下载链接】pkg 项目地址: https://gitcode.com/gh_mirrors/pkg/pkg 在现代 JavaScript 开发中&#xff0c;包管理工具的选择直接影响项目构建效率和依赖管理体验。gh_mirrors/pkg…...

通义千问3-Reranker-0.6B实战教程:结合Embedding模型的两级检索架构

通义千问3-Reranker-0.6B实战教程&#xff1a;结合Embedding模型的两级检索架构 1. 认识通义千问重排序模型 Qwen3-Reranker-0.6B 是阿里云通义千问团队推出的新一代文本重排序模型&#xff0c;专门为解决文本检索和排序任务而设计。这个模型就像一个智能的"裁判"&…...

Qwen3-ASR-1.7B效果展示:英文技术讲座→专业术语保留→结构化摘要生成

Qwen3-ASR-1.7B效果展示&#xff1a;英文技术讲座→专业术语保留→结构化摘要生成 1. 引言&#xff1a;当AI“听懂”一场技术讲座 想象一下这个场景&#xff1a;你刚刚参加完一场全英文的技术分享会&#xff0c;演讲者语速飞快&#xff0c;夹杂着大量“Transformer”、“Atte…...

Z-Image-Turbo-rinaiqiao-huiyewunv惊艳效果:辉夜大小姐手持团扇+浮世绘背景风格迁移

Z-Image-Turbo-rinaiqiao-huiyewunv惊艳效果&#xff1a;辉夜大小姐手持团扇浮世绘背景风格迁移 1. 项目概述 Z-Image Turbo (辉夜大小姐-日奈娇)是基于Tongyi-MAI Z-Image底座模型开发的专属二次元人物绘图工具。该工具通过注入辉夜大小姐(日奈娇)微调权重&#xff0c;实现了…...

Kafka Consumer Group 详解:原理、机制与应用实践

Kafka Consumer Group 详解&#xff1a;原理、机制与应用实践前言什么是 Consumer Group&#xff1f;核心特征Consumer Group 的核心作用1. 实现发布-订阅模式2. 实现消息队列模式3. 消费能力的水平扩展4. 故障自动转移Consumer Group 的工作原理核心组件工作流程分区分配策略1…...

2026 论文写作工具实测:Paperxie 领衔 9 款 AI 工具,搞定初稿 / 绘图 / 排版 / AI 率全流程

paperxie-免费查重复率aigc检测/开题报告/毕业论文/智能排版/文献综述/aippthttps://www.paperxie.cn/ai/dissertationhttps://www.paperxie.cn/ai/dissertation 毕业季的论文焦虑&#xff0c;从来都不是「不会写」&#xff0c;而是「写不完、写不好、通不过」。从选题卡壳到格…...

电-气-热综合能源系统节点能价计算方法研究

基本文献复现-计及碳排放成本的电_气_热综合能源系统节点能价计算方法研究 真正做到了电热气潮流耦合&#xff0c;很适合综合能源系统建模的初学者&#xff0c;配合复现论文。 运行程序HeatGasPowerCombination即可。 每个系统模型都有专门的文档讲解&#xff0c;程序注释齐全。…...

直驱风机Simulink仿真模型与永磁直驱式风力发电系统整体仿真:380V与690V双电压仿真...

直驱风机simulink仿真模型&#xff0c;永磁直驱式风力发电系统 matlab/simulink整体仿真&#xff0c;有380V和690V两个仿真&#xff0c;波形如图&#xff0c;现货有2018 和 2021 两个版本&#xff0c;可导出2015b-2022版本&#xff0c;有模型说明和文献直驱风机在风电场的应用这…...

5分钟搞定!用GISSaaS.MapDownloader一键下载高德/百度/腾讯地图离线包(附详细配置截图)

高效获取多平台地图数据&#xff1a;GISSaaS.MapDownloader全流程指南 在GIS开发或户外探险场景中&#xff0c;离线地图数据的重要性不言而喻。无论是应对网络不稳定环境&#xff0c;还是进行大规模地理数据分析&#xff0c;本地存储的地图资源都能显著提升工作效率。传统手动下…...