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)站的支持!
上一篇:詳解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
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)