zoukankan      html  css  js  c++  java
  • C#使用Redis的基本操作

    https://www.jb51.net/article/117194.htm

    一,引入dll

      1.ServiceStack.Common.dll

      2.ServiceStack.Interfaces.dll

      3.ServiceStack.Redis.dll

      4.ServiceStack.Text.dll

    二,修改配置文件

      在你的配置文件中加入如下的代码:

    1
    2
    3
    <appSettings>
     <add key="RedisPath" value="127.0.0.1:6379"/>  todo:这里配置自己redis的ip地址和端口号
     </appSettings>

    二,用到的工具类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ServiceStack.Redis;
    namespace RedisDemo
    {
      /// <summary>
      /// RedisManager类主要是创建链接池管理对象的
      /// </summary>
      public class RedisManager
      {
        /// <summary>
        /// redis配置文件信息
        /// </summary>
        private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];
        private static PooledRedisClientManager _prcm;
        /// <summary>
        /// 静态构造方法,初始化链接池管理对象
        /// </summary>
        static RedisManager()
        {
          CreateManager();
        }
        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static void CreateManager()
        {
          _prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
        }
        private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
        {
          //WriteServerList:可写的Redis链接地址。
          //ReadServerList:可读的Redis链接地址。
          //MaxWritePoolSize:最大写链接数。
          //MaxReadPoolSize:最大读链接数。
          //AutoStart:自动重启。
          //LocalCacheTime:本地缓存到期时间,单位:秒。
          //RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。
          //RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应
          // 支持读写分离,均衡负载
          return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
          {
            MaxWritePoolSize = 5, // “写”链接池链接数
            MaxReadPoolSize = 5, // “读”链接池链接数
            AutoStart = true,
          });
        }
        private static IEnumerable<string> SplitString(string strSource, string split)
        {
          return strSource.Split(split.ToArray());
        }
        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static IRedisClient GetClient()
        {
          if (_prcm == null)
          {
            CreateManager();
          }
          return _prcm.GetClient();
        }
      }
    }

    三,main方法执行存储操作与读取操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ServiceStack.Redis;
    using ServiceStack.Redis.Support;
    namespace RedisDemo
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            //获取Redis操作接口
            IRedisClient Redis = RedisManager.GetClient();
            //放入内存
            Redis.Set<string>("my_name", "小张");
            Redis.Set<int>("my_age", 12);
            //保存到硬盘
            Redis.Save();
            //释放内存
            Redis.Dispose();
            //取出数据
            Console.WriteLine("取出刚才存进去的数据 我的Name:{0}; 我的Age:{1}.",
              Redis.Get<string>("my_name"), Redis.Get<int>("my_age"));
            Console.ReadKey();
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message.ToString());
            Console.ReadKey();
          }
        }
      }
    }

    完活,下面是运行后的结果

    以上所述是小编给大家介绍的C#使用Redis的基本操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

                            <div class="art_xg">
                                <b>您可能感兴趣的文章:</b><ul><li><a href="/article/116535.htm" title="在c#中使用servicestackredis操作redis的实例代码" target="_blank">在c#中使用servicestackredis操作redis的实例代码</a></li><li><a href="/article/201034.htm" title="c#使用csredis操作redis的示例" target="_blank">c#使用csredis操作redis的示例</a></li><li><a href="/article/190208.htm" title="C#中如何使用redis" target="_blank">C#中如何使用redis</a></li><li><a href="/article/190137.htm" title="c#操作Redis的5种基本类型汇总" target="_blank">c#操作Redis的5种基本类型汇总</a></li><li><a href="/article/179641.htm" title="基于C# 写一个 Redis 数据同步小工具" target="_blank">基于C# 写一个 Redis 数据同步小工具</a></li><li><a href="/article/113200.htm" title="C# Redis学习系列(二)Redis基本设置" target="_blank">C# Redis学习系列(二)Redis基本设置</a></li><li><a href="/article/113189.htm" title="C# Redis学习系列(一)Redis下载安装使用" target="_blank">C# Redis学习系列(一)Redis下载安装使用</a></li><li><a href="/article/113168.htm" title="C#实现redis读写的方法" target="_blank">C#实现redis读写的方法</a></li><li><a href="/article/59432.htm" title="Redis总结笔记(二):C#连接Redis简单例子" target="_blank">Redis总结笔记(二):C#连接Redis简单例子</a></li><li><a href="/article/207514.htm" title="C# 通过ServiceStack 操作Redis" target="_blank">C# 通过ServiceStack 操作Redis</a></li></ul>
                            </div>
    
                        </div>
  • 相关阅读:
    npm 中设置环境NODE_ENV变量,判断失败打印process.env.NODE_ENV确实是"development",但是判断process.env.NODE_ENV === "development" 是false
    NuxtJS踩坑日记,一步一步爬出我自己挖的坑。
    Django模型层1
    Django模板层2
    Centos 6.5 版本的下载教程
    上传文件到github
    CSS完整
    前端之JavaScript
    CSS
    多表 查询习题
  • 原文地址:https://www.cnblogs.com/sunny3158/p/14552931.html
Copyright © 2011-2022 走看看