内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何使用Enyim.Caching訪問Memcached-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)如何使用Enyim.Caching訪問Memcached,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、凌河網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為凌河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

(1)首先下載EnyimMemcached(文件名:EnyimMemcached-master.zip)。

進入網(wǎng)址https://github.com/enyim ,可以看到如下界面,并選擇“EnyimMemcached”

如何使用Enyim.Caching訪問Memcached

進入如下頁面,或者直接訪問:https://github.com/enyim/EnyimMemcached,點擊右側(cè)的“Download ZIP”,得到文件“EnyimMemcached-master.zip”

如何使用Enyim.Caching訪問Memcached

(2)將“EnyimMemcached-master.zip”解壓出來,如下圖

注:我們可以用“*.dll”進行搜索一下,發(fā)現(xiàn)并沒有Enyim.Caching.dll,所以需要我們自己生成。

用Visual Studio 2010打開“Enyim.Caching.sln”文件

如何使用Enyim.Caching訪問Memcached

在打開的過程中,我遇到了(好幾次)下面的提示,以我目前的水平,還不能確切的明白 “這個提示到底會發(fā)生什么事情”,所以我將它忽略了,如果有明白的朋友,可以告訴我啊

如何使用Enyim.Caching訪問Memcached

直接,我們按一下F6(生成解決方案),會發(fā)現(xiàn)如下錯誤

如何使用Enyim.Caching訪問Memcached

針對上面的問題,我們可以查看右側(cè)的“解決方案資源管理器”內(nèi)各個項目的引用信息,發(fā)現(xiàn)

-->Enyim.Caching.Log4NetAdaper項目中l(wèi)og4net的引用丟失

-->MemcachedTest項目中nuit.framework、nuit.mocks的引用丟失

如何使用Enyim.Caching訪問Memcached

對于這個問題,我們可以Nuget來解決一下

如何使用Enyim.Caching訪問Memcached

再看一下,發(fā)現(xiàn)錯誤更多,如下圖,發(fā)現(xiàn)原來是“NUit的命名空間沒有找到”

如何使用Enyim.Caching訪問Memcached

再用Nuget解決一下這個NUnit的問題,搜索nunit.mocks,安裝一下,再按F6(生成解決方案),發(fā)現(xiàn)成功了。

如何使用Enyim.Caching訪問Memcached

(3)生成Enyim.Caching.dll的Release版本,在Enyim.Caching的Bin\Release目錄下找到Enyim.Caching.dll文件

如何使用Enyim.Caching訪問Memcached

Enyim.Caching\bin\Release

如何使用Enyim.Caching訪問Memcached

(4)新建一個“控制臺應(yīng)用程序”,將Enyim.Caching.dll放到項目的bin目錄下

如何使用Enyim.Caching訪問Memcached

LearningEnyimMemcached\bin

如何使用Enyim.Caching訪問Memcached

添加Enyim.Caching的引用

如何使用Enyim.Caching訪問Memcached

(5)添加一個“應(yīng)用程序配置文件”,文件名是App.config

如何使用Enyim.Caching訪問Memcached

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的引用了。

如何使用Enyim.Caching訪問Memcached

針對這個問題,打開項目的屬性,將項目的“目標(biāo)框架”(由原來的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解決方案),就成功了。

如何使用Enyim.Caching訪問Memcached

按F5(啟動調(diào)試),看到如下結(jié)果

如何使用Enyim.Caching訪問Memcached

(7)上面的代碼確實能夠運行,但是如果對自己要求更高一些,應(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");
                
            }

因為MemcachedClient類實現(xiàn)了IDisposable接口

參考網(wǎng)址: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;
        }
    }
}

關(guān)于“如何使用Enyim.Caching訪問Memcached”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

新聞標(biāo)題:如何使用Enyim.Caching訪問Memcached-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article8/ccssop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、營銷型網(wǎng)站建設(shè)、域名注冊、商城網(wǎng)站、網(wǎng)站設(shè)計公司、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
田阳县| 平乡县| 定陶县| 黄骅市| 佳木斯市| 温宿县| 团风县| 永嘉县| 巴林右旗| 古丈县| 江山市| 天柱县| 井研县| 铁岭县| 河北省| 东海县| 穆棱市| 越西县| 平武县| 苍山县| 临澧县| 洪洞县| 德兴市| 嘉义市| 新蔡县| 九江县| 凉城县| 蚌埠市| 饶阳县| 黑龙江省| 龙里县| 磐石市| 工布江达县| 孝义市| 乌恰县| 娄烦县| 昆山市| 潜山县| 法库县| 阿克陶县| 鄂温|