欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#使用Redis的基本操作

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

一,引入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)站的支持!

上一篇:詳解C#中的System.Timers.Timer定時(shí)器的使用和定時(shí)自動(dòng)清理內(nèi)存應(yīng)用

欄    目:C#教程

下一篇:C#圖片處理類分享

本文標(biāo)題:C#使用Redis的基本操作

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5627.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有