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();
  }
相关推荐
网管NO.16 分钟前
SQL 模糊查询 + NULL 空值。LIKE 通配符 % 和_、IS NULL
数据库
Mr. zhihao7 分钟前
Redis 内存管理深度解析:过期删除与内存淘汰策略
数据库·redis·缓存
Solis程序员8 分钟前
分层缓存调度:削峰控压下的 Feed 流高性能设计
缓存
九皇叔叔9 分钟前
高斯性能分析【第一天】单表执行计划分析
java·数据库·性能分析·执行计划·gauss
Mr. zhihao9 分钟前
Redis 持久化完全指南:从 RDB、AOF 到 MP-AOF
redis
難釋懷21 分钟前
Redis内存回收-过期key处理
数据库·redis·缓存
KaMeidebaby22 分钟前
卡梅德生物技术快报|PROTAC 药物降解蛋白原理及数据库平台开发全流程
前端·数据库·其他·百度·新浪微博
鱼鳞_23 分钟前
苍穹外卖-Day05(Redis)
java·redis
是码龙不是码农30 分钟前
数据库主键选型:为什么别用自增 ID?
java·数据库
IpdataCloud35 分钟前
企业IT管理中,如何通过IP地址查询定位快速溯源异常终端?用IP离线库实现
服务器·网络·数据库·tcp/ip