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

Redis远程链接应用案例

1.配置文件设置

打开配置文件redis.windows.conf,配置以下内容:

1.bind 0.0.0.0(设置所有IP可访问)

2.requirepass 1234.com(密码设置)

3.protected-mode no(远程可访问)

2.防火墙配置入站规则,端口号6379

3.安全组配置,开放6379

4.C#代码案例-字符串读写

 public static void WriteandReadString(){// 连接字符串,根据实际Redis地址和端口调整string connectionString = "41.162.118.219:6379,password=1234.com";ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(connectionString);IDatabase redis = connection.GetDatabase();// 设置字符串键值对redis.StringSet("myStringKey", "Hello, Redis!");// 获取字符串值string value = redis.StringGet("myStringKey");Console.WriteLine($"获取的值为: {value}");connection.Close();}

5.C#代码案例-哈希读写

 public static void WriteandReadHash(){// 连接字符串,根据实际Redis地址和端口调整string connectionString = "41.162.118.219:6379,password=1234.com";ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(connectionString);IDatabase redis = connection.GetDatabase();// 设置哈希字段值redis.HashSet("myHashKey", new HashEntry[] {new HashEntry("field1", "value3"),new HashEntry("field2", "value2")});// 获取单个哈希字段值string field1Value = redis.HashGet("myHashKey", "field1");Console.WriteLine($"哈希字段field1的值为: {field1Value}");// 获取所有哈希字段和值HashEntry[] hashEntries = redis.HashGetAll("myHashKey");foreach (var entry in hashEntries){Console.WriteLine($"哈希字段: {entry.Name}, 值: {entry.Value}");}connection.Close();}

6.C#代码案例-集合读写

  public static void WriteandReadJiHe(){// 连接字符串,根据实际Redis地址和端口调整string connectionString = "41.162.118.219:6379,password=1234.com";ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(connectionString);IDatabase redis = connection.GetDatabase();// 向集合添加元素redis.SetAdd("mySetKey", "member1");redis.SetAdd("mySetKey", "member2");// 获取集合所有成员RedisValue[] members = redis.SetMembers("mySetKey");foreach (var member in members){Console.WriteLine($"集合成员: {member}");}connection.Close();}

7.有序集合读写

  public static void WriteandReadSortedSet(){// 连接字符串,根据实际Redis地址和端口调整string connectionString = "41.162.118.209:6379,password=1234.com";ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(connectionString);IDatabase redis = connection.GetDatabase();// 向有序集合添加元素及分值redis.SortedSetAdd("mySortedSetKey", "item1", 5);redis.SortedSetAdd("mySortedSetKey", "item2", 3);// 获取元素排名//long rank = 0;// rank = redis.SortedSetRank("mySortedSetKey", "item2");//Console.WriteLine($"元素item2的排名为: {rank}");//connection.Close();long? rank = 0;rank = redis.SortedSetRank("mySortedSetKey", "item2");Console.WriteLine($"元素的排名为: {rank}");connection.Close();}


http://www.mrgr.cn/news/100398.html

相关文章:

  • CMCC RAX3000M CH EC 算力版刷机(中国移动 RAX3000M 算力版)刷机
  • 【STL】unordered_map
  • 毕业设计-基于机器学习入侵检测系统
  • 12.thinkphp验证
  • 毕业设计-基于深度学习的入侵检测系统
  • MYSQL 常用字符串函数 和 时间函数详解
  • RabbitMQ安装流程(Windows环境)
  • K8S Service 原理、图例——深度好文
  • 高性能服务器配置经验指南1——刚配置好服务器应该做哪些事
  • 详解最新链路追踪skywalking框架介绍、架构、环境本地部署配置、整合微服务springcloudalibaba 、日志收集、自定义链路追踪、告警等
  • 深入理解N皇后问题:从DFS到对角线优化
  • (六)RestAPI 毛子(外部导入打卡/游标分页/Refit/Http resilience/批量提交/Quartz后台任务/Hateoas Driven)
  • springboot应用使用shell脚本打包成部署压缩包(支持xjar)
  • 【深度学习】【目标检测】【Ultralytics-YOLO系列】YOLOV3核心文件common.py解读
  • git tag使用场景和实践
  • C语言HashTable基本理解
  • TCP专题
  • IT社团分析预测项目(pandas、numpy、sklearn)
  • Vue前端学习笔记
  • ​Stable Diffusion:Diffusion Model