這篇文章將為大家詳細(xì)講解有關(guān)如何使用Enyim.Caching訪問Memcached,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、凌河網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為凌河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。(1)首先下載EnyimMemcached(文件名:EnyimMemcached-master.zip)。
進(jìn)入網(wǎng)址https://github.com/enyim ,可以看到如下界面,并選擇“EnyimMemcached”
進(jìn)入如下頁面,或者直接訪問:https://github.com/enyim/EnyimMemcached,點(diǎn)擊右側(cè)的“Download ZIP”,得到文件“EnyimMemcached-master.zip”
(2)將“EnyimMemcached-master.zip”解壓出來,如下圖
注:我們可以用“*.dll”進(jìn)行搜索一下,發(fā)現(xiàn)并沒有Enyim.Caching.dll,所以需要我們自己生成。
用Visual Studio 2010打開“Enyim.Caching.sln”文件
在打開的過程中,我遇到了(好幾次)下面的提示,以我目前的水平,還不能確切的明白 “這個(gè)提示到底會(huì)發(fā)生什么事情”,所以我將它忽略了,如果有明白的朋友,可以告訴我啊
直接,我們按一下F6(生成解決方案),會(huì)發(fā)現(xiàn)如下錯(cuò)誤
針對(duì)上面的問題,我們可以查看右側(cè)的“解決方案資源管理器”內(nèi)各個(gè)項(xiàng)目的引用信息,發(fā)現(xiàn)
-->Enyim.Caching.Log4NetAdaper項(xiàng)目中l(wèi)og4net的引用丟失
-->MemcachedTest項(xiàng)目中nuit.framework、nuit.mocks的引用丟失
對(duì)于這個(gè)問題,我們可以Nuget來解決一下
再看一下,發(fā)現(xiàn)錯(cuò)誤更多,如下圖,發(fā)現(xiàn)原來是“NUit的命名空間沒有找到”
再用Nuget解決一下這個(gè)NUnit的問題,搜索nunit.mocks,安裝一下,再按F6(生成解決方案),發(fā)現(xiàn)成功了。
(3)生成Enyim.Caching.dll的Release版本,在Enyim.Caching的Bin\Release目錄下找到Enyim.Caching.dll文件
Enyim.Caching\bin\Release
(4)新建一個(gè)“控制臺(tái)應(yīng)用程序”,將Enyim.Caching.dll放到項(xiàng)目的bin目錄下
LearningEnyimMemcached\bin
添加Enyim.Caching的引用
(5)添加一個(gè)“應(yīng)用程序配置文件”,文件名是App.config
App.config中的配置如下
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> </configSections> <enyim.com> <memcached protocol="Binary"> <servers> <!-- make sure you use the same ordering of nodes in every configuration you have --> <add address="192.168.100.85" port="11211" /> <add address="192.168.100.63" port="11211" /> </servers> <socketPool minPoolSize="10" maxPoolSize="20" connectionTimeout="00:00:10" deadTimeout="00:00:10" /> <keyTransformer type="Enyim.Caching.Memcached.TigerHashKeyTransformer, Enyim.Caching" /> </memcached> </enyim.com> </configuration>
(6)Program.cs文件中的代碼
如下
using System; using Enyim.Caching; using Enyim.Caching.Memcached; namespace LearningEnyimMemcached { class Program { static void Main(string[] args) { // create a MemcachedClient in your application // you can cache the client in a static variable or just recreate it every time MemcachedClient mc = new MemcachedClient(); // store a string in the cache mc.Store(StoreMode.Set, "MyKey", "Hello"); // retrieve the item from the cache Console.WriteLine(mc.Get("MyKey")); // store some other items mc.Store(StoreMode.Set, "D1", 1234L); mc.Store(StoreMode.Set, "D2", DateTime.Now); mc.Store(StoreMode.Set, "D3", true); mc.Store(StoreMode.Set, "D4", new Product()); mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Console.WriteLine("D1: {0}", mc.Get("D1")); Console.WriteLine("D2: {0}", mc.Get("D2")); Console.WriteLine("D3: {0}", mc.Get("D3")); Console.WriteLine("D4: {0}", mc.Get("D4")); byte[] tmp = mc.Get<byte[]>("D5"); // delete them from the cache mc.Remove("D1"); mc.Remove("D2"); mc.Remove("D3"); mc.Remove("D4"); // add an item which is valid for 10 mins mc.Store(StoreMode.Set, "D4", new Product(), new TimeSpan(0, 10, 0)); Console.WriteLine("D4: {0}", mc.Get("D4")); Console.ReadLine(); } // objects must be serializable to be able to store them in the cache [Serializable] class Product { public double Price = 1.24; public string Name = "Mineral Water"; public override string ToString() { return String.Format("Product {{{0}: {1}}}", this.Name, this.Price); } } } }
再按一下F6,發(fā)現(xiàn)有11個(gè)錯(cuò)誤,說是“未能找到Enyim的命名空間”,可是,我已經(jīng)添加了Enyim.Caching的引用了。
針對(duì)這個(gè)問題,打開項(xiàng)目的屬性,將項(xiàng)目的“目標(biāo)框架”(由原來的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解決方案),就成功了。
按F5(啟動(dòng)調(diào)試),看到如下結(jié)果
(7)上面的代碼確實(shí)能夠運(yùn)行,但是如果對(duì)自己要求更高一些,應(yīng)該這樣寫
using(MemcachedClient client = new MemcachedClient()) { // Store the record client.Store(StoreMode.Set, "currentTime", DateTime.Now.ToString()); // Retrieve the value string value = client.Get<string>("currentTime"); }
因?yàn)镸emcachedClient類實(shí)現(xiàn)了IDisposable接口
參考網(wǎng)址:http://deanhume.com/home/blogpost/memcached-for-c----a-walkthrough/62
同樣,在這篇文章中,作者也寫了一個(gè)CacheLayer.cs文件,對(duì)MemcachedClient進(jìn)行了封裝(a little wrapper),但是有一個(gè)問題需要注意:作者的寫的這個(gè)CacheLayer.cs類只能夠在Northscale的1.4.5版本的Memcached下正常運(yùn)行。
CacheLayer.cs的代碼如下
namespace MemcacheExample.Cache { using System; using Enyim.Caching; using Enyim.Caching.Memcached; public class CacheLayer { private static readonly MemcachedClient Cache = new MemcachedClient(); /// <summary> /// Retrieve cached item /// </summary> /// <typeparam name="T">Type of cached item</typeparam> /// <param name="key">Name of cached item</param> /// <returns>Cached item as type</returns> public static T Get<T>(string key) where T : class { try { return (T) Cache.Get(key); } catch { return null; } } /// <summary> /// Insert value into the cache using /// appropriate name/value pairs /// </summary> /// <typeparam name="T">Type of cached item</typeparam> /// <param name="objectToCache">Item to be cached</param> /// <param name="key">Name of item</param> /// <param name="cacheDuration">Duration of the cache.</param> public static void Add<T>(T objectToCache, string key, int cacheDuration) where T : class { Cache.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration)); } /// <summary> /// Insert value into the cache using /// appropriate name/value pairs /// </summary> /// <param name="objectToCache">Item to be cached</param> /// <param name="key">Name of item</param> /// <param name="cacheDuration">Duration of the cache.</param> public static void Add(object objectToCache, string key, int cacheDuration) { Cache.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration)); } /// <summary> /// Remove item from cache /// </summary> /// <param name="key">Name of cached item</param> public static void Remove(string key) { Cache.Remove(key); } /// <summary> /// Clears all stored objects from memory. /// </summary> public static void ClearAll() { Cache.FlushAll(); } /// <summary> /// Check for item in cache /// </summary> /// <param name="key">Name of cached item</param> /// <returns>A boolean if the object exists</returns> public static bool Exists(string key) { return Cache.Get(key) != null; } } }
關(guān)于“如何使用Enyim.Caching訪問Memcached”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
新聞標(biāo)題:如何使用Enyim.Caching訪問Memcached-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://jinyejixie.com/article8/ccssop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、域名注冊(cè)、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容