這篇文章將為大家詳細講解有關如何使用Enyim.Caching訪問Memcached,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創新互聯專注于企業網絡營銷推廣、網站重做改版、凌河網站定制設計、自適應品牌網站建設、H5開發、商城網站開發、集團公司官網建設、成都外貿網站制作、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為凌河等各大城市提供網站開發制作服務。(1)首先下載EnyimMemcached(文件名:EnyimMemcached-master.zip)。
進入網址https://github.com/enyim ,可以看到如下界面,并選擇“EnyimMemcached”
進入如下頁面,或者直接訪問:https://github.com/enyim/EnyimMemcached,點擊右側的“Download ZIP”,得到文件“EnyimMemcached-master.zip”
(2)將“EnyimMemcached-master.zip”解壓出來,如下圖
注:我們可以用“*.dll”進行搜索一下,發現并沒有Enyim.Caching.dll,所以需要我們自己生成。
用Visual Studio 2010打開“Enyim.Caching.sln”文件
在打開的過程中,我遇到了(好幾次)下面的提示,以我目前的水平,還不能確切的明白 “這個提示到底會發生什么事情”,所以我將它忽略了,如果有明白的朋友,可以告訴我啊
直接,我們按一下F6(生成解決方案),會發現如下錯誤
針對上面的問題,我們可以查看右側的“解決方案資源管理器”內各個項目的引用信息,發現
-->Enyim.Caching.Log4NetAdaper項目中log4net的引用丟失
-->MemcachedTest項目中nuit.framework、nuit.mocks的引用丟失
對于這個問題,我們可以Nuget來解決一下
再看一下,發現錯誤更多,如下圖,發現原來是“NUit的命名空間沒有找到”
再用Nuget解決一下這個NUnit的問題,搜索nunit.mocks,安裝一下,再按F6(生成解決方案),發現成功了。
(3)生成Enyim.Caching.dll的Release版本,在Enyim.Caching的Bin\Release目錄下找到Enyim.Caching.dll文件
Enyim.Caching\bin\Release
(4)新建一個“控制臺應用程序”,將Enyim.Caching.dll放到項目的bin目錄下
LearningEnyimMemcached\bin
添加Enyim.Caching的引用
(5)添加一個“應用程序配置文件”,文件名是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,發現有11個錯誤,說是“未能找到Enyim的命名空間”,可是,我已經添加了Enyim.Caching的引用了。
針對這個問題,打開項目的屬性,將項目的“目標框架”(由原來的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解決方案),就成功了。
按F5(啟動調試),看到如下結果
(7)上面的代碼確實能夠運行,但是如果對自己要求更高一些,應該這樣寫
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"); }
因為MemcachedClient類實現了IDisposable接口
參考網址:http://deanhume.com/home/blogpost/memcached-for-c----a-walkthrough/62
同樣,在這篇文章中,作者也寫了一個CacheLayer.cs文件,對MemcachedClient進行了封裝(a little wrapper),但是有一個問題需要注意:作者的寫的這個CacheLayer.cs類只能夠在Northscale的1.4.5版本的Memcached下正常運行。
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; } } }
關于“如何使用Enyim.Caching訪問Memcached”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
創新互聯www.cdcxhl.cn,專業提供香港、美國云服務器,動態BGP最優骨干路由自動選擇,持續穩定高效的網絡助力業務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節活動現已開啟,新人活動云服務器買多久送多久。
新聞標題:如何使用Enyim.Caching訪問Memcached-創新互聯
當前網址:http://vcdvsql.cn/article8/ccssop.html
成都網站建設公司_創新互聯,為您提供網頁設計公司、營銷型網站建設、域名注冊、商城網站、網站設計公司、微信小程序
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯