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

    一,引入dll

      1.ServiceStack.Common.dll

      2.ServiceStack.Interfaces.dll

      3.ServiceStack.Redis.dll

      4.ServiceStack.Text.dll

    二,修改配置文件

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

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


    二,用到的工具类

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

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

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

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

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

  • 相关阅读:
    JAVA-jar包下载地址
    JAVA-Eclipse中web-inf和meta-inf文件夹
    【转载】JAVA-dynamic web module与tomcat
    判断二叉树是不是平衡
    二叉树的深度
    二叉搜索树的后序遍历序列
    数对之差的最大值
    字符串的组合
    求二元查找树的镜像
    字符串的排列
  • 原文地址:https://www.cnblogs.com/anyihen/p/12830509.html
Copyright © 2011-2022 走看看