欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用Redis的基本操作

 更新時(shí)間:2017年06月26日 15:24:08   作者:請(qǐng)叫我小馮哥哥  
這篇文章主要介紹了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地址和端口號(hào)
  </appSettings>

二,用到的工具類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
namespace RedisDemo
{
  /// <summary>
  /// RedisManager類主要是創(chuàng)建鏈接池管理對(duì)象的
  /// </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)造方法,初始化鏈接池管理對(duì)象
    /// </summary>
    static RedisManager()
    {
      CreateManager();
    }
    /// <summary>
    /// 創(chuàng)建鏈接池管理對(duì)象
    /// </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:自動(dòng)重啟。
      //LocalCacheTime:本地緩存到期時(shí)間,單位:秒。
      //RecordeLog:是否記錄日志,該設(shè)置僅用于排查redis運(yùn)行時(shí)出現(xiàn)的問(wèn)題,如redis工作正常,請(qǐng)關(guān)閉該項(xiàng)。
      //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>
    /// 客戶端緩存操作對(duì)象
    /// </summary>
    public static IRedisClient GetClient()
    {
      if (_prcm == null)
      {
        CreateManager();
      }
      return _prcm.GetClient();
    }
  }
}

三,main方法執(zhí)行存儲(chǔ)操作與讀取操作

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();
      }
    }
  }
}

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

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

相關(guān)文章

  • C#檢查指定對(duì)象是否存在于ArrayList集合中的方法

    C#檢查指定對(duì)象是否存在于ArrayList集合中的方法

    這篇文章主要介紹了C#檢查指定對(duì)象是否存在于ArrayList集合中的方法,涉及C#中Contains方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#利用iTextSharp添加PDF水印

    C#利用iTextSharp添加PDF水印

    這篇文章主要為大家詳細(xì)介紹了C#利用iTextSharp添加PDF水印的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • C# 遞歸算法詳解

    C# 遞歸算法詳解

    什么是遞歸函數(shù)/方法?任何一個(gè)方法既可以調(diào)用其他方法也可以調(diào)用自己,而當(dāng)這個(gè)方法調(diào)用自己時(shí),我們就叫它遞歸函數(shù)或遞歸算法,接下來(lái)詳細(xì)介紹需要了解的朋友可以參考下
    2021-11-11
  • C#實(shí)現(xiàn)工廠方法模式

    C#實(shí)現(xiàn)工廠方法模式

    這篇文章介紹了C#實(shí)現(xiàn)工廠模式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • PropertyGrid自定義控件使用詳解

    PropertyGrid自定義控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了PropertyGrid自定義控件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#表達(dá)式樹(shù)講解

    C#表達(dá)式樹(shù)講解

    本文詳細(xì)講解了C#表達(dá)式樹(shù)的創(chuàng)建、生成和使用,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • C#線程倒計(jì)時(shí)器源碼分享

    C#線程倒計(jì)時(shí)器源碼分享

    這篇文章主要為大家分享了C#線程倒計(jì)時(shí)器源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C#正則檢測(cè)字符串是否字母數(shù)字混編的方法

    C#正則檢測(cè)字符串是否字母數(shù)字混編的方法

    這篇文章主要介紹了C#正則檢測(cè)字符串是否字母數(shù)字混編的方法,涉及C#正則判定字符串的使用技巧,需要的朋友可以參考下
    2015-06-06
  • c# 類型轉(zhuǎn)換

    c# 類型轉(zhuǎn)換

    CLR最重要的特性之一就是類型安全性。在運(yùn)行時(shí),CLR總是知道一個(gè)對(duì)象是什么類型。調(diào)用GetType方法可以返回類型
    2012-10-10
  • C#實(shí)現(xiàn)記事本查找與替換功能

    C#實(shí)現(xiàn)記事本查找與替換功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)記事本查找與替換功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評(píng)論