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();
  }
相关推荐
CarIise28 分钟前
MySQL数据库表关系与多表查询:从表间关系设计到JOIN联表查询
数据库·mysql
samble29 分钟前
从零搭建工业控制系统(二十一):状态管理系统——中心状态对象设计
c#·wpf·mvvm·状态管理·工业控制
网络工程小王42 分钟前
【LLM开发实验】FastAPI 学习笔记
数据库·笔记·学习·mysql·fastapi
Aime_Perfect1 小时前
sqlserver always on
服务器·数据库·sqlserver
摘星编程1 小时前
从“数据出库“到“模型入库“:DolphinDB 库内机器学习全流程实践
数据库
Sagittarius_A*1 小时前
【好靶场】SQL注入-时间盲注
数据库·sql
神明不懂浪漫2 小时前
【第二章】库、表、增删改查操作
开发语言·数据库·经验分享·笔记
其实防守也摸鱼2 小时前
OpenClaw 深度解析:从原理到实战的完整指南
运维·服务器·数据库·github·copilot
hzp6662 小时前
doris学习6:参数调优
数据库·doris·参数·数据库调优
俊昭喜喜里2 小时前
C#中的func<>
java·前端·c#