您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用Enyim.Caching訪問Memcached,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
(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”文件
在打開的過程中,我遇到了(好幾次)下面的提示,以我目前的水平,還不能確切的明白 “這個提示到底會發(fā)生什么事情”,所以我將它忽略了,如果有明白的朋友,可以告訴我啊
直接,我們按一下F6(生成解決方案),會發(fā)現(xiàn)如下錯誤
針對上面的問題,我們可以查看右側(cè)的“解決方案資源管理器”內(nèi)各個項(xiàng)目的引用信息,發(fā)現(xiàn)
-->Enyim.Caching.Log4NetAdaper項(xiàng)目中l(wèi)og4net的引用丟失
-->MemcachedTest項(xiàng)目中nuit.framework、nuit.mocks的引用丟失
對于這個問題,我們可以Nuget來解決一下
再看一下,發(fā)現(xiàn)錯誤更多,如下圖,發(fā)現(xiàn)原來是“NUit的命名空間沒有找到”
再用Nuget解決一下這個NUnit的問題,搜索nunit.mocks,安裝一下,再按F6(生成解決方案),發(fā)現(xiàn)成功了。
(3)生成Enyim.Caching.dll的Release版本,在Enyim.Caching的Bin\Release目錄下找到Enyim.Caching.dll文件
Enyim.Caching\bin\Release
(4)新建一個“控制臺應(yīng)用程序”,將Enyim.Caching.dll放到項(xiàng)目的bin目錄下
LearningEnyimMemcached\bin
添加Enyim.Caching的引用
(5)添加一個“應(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個錯誤,說是“未能找到Enyim的命名空間”,可是,我已經(jīng)添加了Enyim.Caching的引用了。
針對這個問題,打開項(xiàng)目的屬性,將項(xiàng)目的“目標(biāo)框架”(由原來的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解決方案),就成功了。
按F5(啟動調(diào)試),看到如下結(jié)果
(7)上面的代碼確實(shí)能夠運(yùn)行,但是如果對自己要求更高一些,應(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
同樣,在這篇文章中,作者也寫了一個CacheLayer.cs文件,對MemcachedClient進(jìn)行了封裝(a little wrapper),但是有一個問題需要注意:作者的寫的這個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)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。