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();
  }
相关推荐
6极地诈唬20 分钟前
【sqlite】WAL初探
数据库·sqlite
PieroPc22 分钟前
用Python Streamlit sqlite3 写一个简单博客
数据库·python·sqlite
啊森要自信25 分钟前
【MySQL 数据库】使用C语言操作MySQL
linux·c语言·开发语言·数据库·mysql
你想考研啊1 小时前
二、redis集群部署(3主3从)
数据库·redis·缓存
顾漂亮1 小时前
Redis深度探索
java·redis·后端·spring·缓存
虚行1 小时前
WPF入门
开发语言·c#·wpf
冉冰学姐1 小时前
SSM考试管理z2zvx(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·学生信息管理
友友马1 小时前
『 QT 』信号-槽 补充: Qt信号槽断开连接与Lambda槽技巧
开发语言·数据库·qt
凡间客2 小时前
5、Python3编程之面向对象
java·服务器·数据库
涛思数据(TDengine)2 小时前
TDengine TSDB 3.3.8.0 上线:SMA、TLS、TDgpt、taosX、taosgen 一次全进化
大数据·数据库·时序数据库·tdengine