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();
  }
相关推荐
老友@41 分钟前
小集合 VS 大集合:MySQL 去重计数性能优化
数据库·mysql·性能优化
声声codeGrandMaster1 小时前
django之优化分页功能(利用参数共存及封装来实现)
数据库·后端·python·django
code_shenbing1 小时前
WPF高级用法示例
c#·wpf·wpf高级
冰茶_1 小时前
WPF之XAML基础
microsoft·微软·c#·.net·wpf·xaml·xamarin
熏鱼的小迷弟Liu2 小时前
【Redis】Redis Zset实现原理:跳表+哈希表的精妙设计
数据库·redis·散列表
淋一遍下雨天2 小时前
Spark Streaming核心编程总结(四)
java·开发语言·数据库
zru_96023 小时前
Windows 安装 MongoDB 教程
数据库·mongodb
数据与后端架构提升之路3 小时前
深度解析如何将图像帧和音频片段特征高效存储到向量数据库 Milvus
数据库·opencv·音视频
20242817李臻4 小时前
李臻20242817_安全文件传输系统项目报告_第9周
数据库·安全
小白考证进阶中4 小时前
0基础可以考MySQL OCP么?备考时间需要多久?
数据库·mysql·开闭原则