最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用Redis的基本操作

 更新時間:2017年06月26日 15:24:08   作者:請叫我小馮哥哥  
這篇文章主要介紹了C#使用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>

二,用到的工具類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
namespace RedisDemo
{
  /// <summary>
  /// RedisManager類主要是創(chuàng)建鏈接池管理對象的
  /// </summary>
  public class RedisManager
  {
    /// <summary>
    /// redis配置文件信息
    /// </summary>
    private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];
    private static PooledRedisClientManager _prcm;
    /// <summary>
    /// 靜態(tài)構(gòu)造方法,初始化鏈接池管理對象
    /// </summary>
    static RedisManager()
    {
      CreateManager();
    }
    /// <summary>
    /// 創(chuàng)建鏈接池管理對象
    /// </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:最大寫鏈接數(shù)。
      //MaxReadPoolSize:最大讀鏈接數(shù)。
      //AutoStart:自動重啟。
      //LocalCacheTime:本地緩存到期時間,單位:秒。
      //RecordeLog:是否記錄日志,該設(shè)置僅用于排查redis運行時出現(xiàn)的問題,如redis工作正常,請關(guān)閉該項。
      //RedisConfigInfo類是記錄redis連接信息,此信息和配置文件中的RedisConfig相呼應(yīng)
      // 支持讀寫分離,均衡負(fù)載 
      return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
      {
        MaxWritePoolSize = 5, // “寫”鏈接池鏈接數(shù) 
        MaxReadPoolSize = 5, // “讀”鏈接池鏈接數(shù) 
        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方法執(zhí)行存儲操作與讀取操作

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();
        //放入內(nèi)存
        Redis.Set<string>("my_name", "小張");
        Redis.Set<int>("my_age", 12);
        //保存到硬盤
        Redis.Save();
        //釋放內(nèi)存
        Redis.Dispose();
        //取出數(shù)據(jù)
        Console.WriteLine("取出剛才存進(jìn)去的數(shù)據(jù) \r\n 我的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();
      }
    }
  }
}

完活,下面是運行后的結(jié)果

以上所述是小編給大家介紹的C#使用Redis的基本操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

秦皇岛市| 曲周县| 龙州县| 皮山县| 永新县| 德兴市| 彭州市| 拉孜县| 清徐县| 云南省| 静宁县| 融水| 天门市| 临沭县| 霍林郭勒市| 平南县| 大庆市| 无为县| 越西县| 炉霍县| 神池县| 新和县| 刚察县| 临清市| 东丽区| 崇义县| 盐山县| 麦盖提县| 恩平市| 苍南县| 卢龙县| 滕州市| 婺源县| 柯坪县| 五峰| 陕西省| 葵青区| 五河县| 思茅市| 共和县| 灵山县|