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類主要是創建鏈接池管理對象的
    /// </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方法執行存儲操作與讀取操作

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("取出剛才存進去的數據 \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();
            }
        }
    }
}

完活,下面是運行后的結果

 

 

redis基礎操作,希望能幫到剛開始接觸redis的朋友。謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM