北极星光 发表于 2018-12-25 12:38:12

#Memcached系列#(6)使用Enyim.Caching访问Memcached的一个C#控制台程序

  这篇文章主要是通过Enyim.Caching来完成访问Memcached。
  

  这篇文章标为“原创”,其实,是从多个地方整合过来的内容;但觉得“转载”也不合适,也并不是完全照搬别人的东西。
  参考网址(不过,感觉它的配置写的乱糟糟的):http://www.cnblogs.com/luyinghuai/archive/2008/08/28/1278200.html
  

  

  (1)首先下载EnyimMemcached(文件名:EnyimMemcached-master.zip)。
  进入网址https://github.com/enyim ,可以看到如下界面,并选择“EnyimMemcached”
http://s5.运维网.com/wyfs02/M02/7A/C3/wKiom1a0DnKx_6ntAAEMY_XhIqY692.png
  进入如下页面,或者直接访问:https://github.com/enyim/EnyimMemcached,点击右侧的“Download ZIP”,得到文件“EnyimMemcached-master.zip”
http://s2.运维网.com/wyfs02/M01/7A/C3/wKioL1a0DyShiz32AAC0BDM1ef8172.png
  

  (2)将“EnyimMemcached-master.zip”解压出来,如下图
  注:我们可以用“*.dll”进行搜索一下,发现并没有Enyim.Caching.dll,所以需要我们自己生成。
  

  用Visual Studio 2010打开“Enyim.Caching.sln”文件
http://s2.运维网.com/wyfs02/M01/7A/C3/wKioL1a0D9Xh10IrAAC7uFwUA4w848.png
  在打开的过程中,我遇到了(好几次)下面的提示,以我目前的水平,还不能确切的明白 “这个提示到底会发生什么事情”,所以我将它忽略了,如果有明白的朋友,可以告诉我啊
http://s4.运维网.com/wyfs02/M00/7A/C4/wKiom1a0ENXBqgyfAAA53eRcRe4360.png
  

  直接,我们按一下F6(生成解决方案),会发现如下错误

http://s5.运维网.com/wyfs02/M01/7A/C3/wKioL1a0EbqDzozjAACvxB6jvDg675.png
  针对上面的问题,我们可以查看右侧的“解决方案资源管理器”内各个项目的引用信息,发现
  -->Enyim.Caching.Log4NetAdaper项目中log4net的引用丢失
  -->MemcachedTest项目中nuit.framework、nuit.mocks的引用丢失
http://s3.运维网.com/wyfs02/M02/7A/C4/wKiom1a0ErmSdAgbAAQYLsAIt28434.gif
  

  对于这个问题,我们可以Nuget来解决一下
http://s4.运维网.com/wyfs02/M00/7A/C3/wKioL1a0FGvT11fvAAjTZGRR9SY926.gif
  

  再看一下,发现错误更多,如下图,发现原来是“NUit的命名空间没有找到”

http://s5.运维网.com/wyfs02/M01/7A/C3/wKioL1a0FQjxUEPJAAC-djDhYY4101.png
  再用Nuget解决一下这个NUnit的问题,搜索nunit.mocks,安装一下,再按F6(生成解决方案),发现成功了。

http://s2.运维网.com/wyfs02/M00/7A/C3/wKioL1a0F7SDKcq_ABKQh9yLEkE114.gif
  

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

http://s3.运维网.com/wyfs02/M00/7A/C4/wKiom1a0GB-yiBuqAAKVNMnryFE067.gif
Enyim.Caching\bin\Releasehttp://s3.运维网.com/wyfs02/M00/7A/C3/wKioL1a0GNGB7WFhAAArS084DbQ350.png
  (4)新建一个“控制台应用程序”,将Enyim.Caching.dll放到项目的bin目录下
http://s4.运维网.com/wyfs02/M01/7A/C4/wKiom1a0HifiLFYwAAExvFyBqTk685.png
LearningEnyimMemcached\binhttp://s5.运维网.com/wyfs02/M01/7A/C4/wKioL1a0HvXSo-kIAAAm3hgaDes109.png
  

  添加Enyim.Caching的引用
http://s4.运维网.com/wyfs02/M00/7A/C4/wKiom1a0HuXTpNW7AAOjJxMorIc778.gif
  (5)添加一个“应用程序配置文件”,文件名是App.config
http://s3.运维网.com/wyfs02/M01/7A/C4/wKiom1a0H7bRtQh1AAljAmMMhvQ795.gif
  

  App.config中的配置如下




   
      
   


   
      
      
      
      
      
      
      
   

  (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("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
      
      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的引用了。
http://s1.运维网.com/wyfs02/M00/7A/C4/wKiom1a0KWiC1AdYAAB4Nu1NJrI414.png
  针对这个问题,打开项目的属性,将项目的“目标框架”(由原来的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解决方案),就成功了。
http://s1.运维网.com/wyfs02/M02/7A/C4/wKiom1a0Ke2h8yCQAAvf7S4sYBY430.gif
  

  按F5(启动调试),看到如下结果
http://s2.运维网.com/wyfs02/M02/7A/C4/wKioL1a0KvuBpe_gAAAj_hToND8193.png
  

  (7)上面的代码确实能够运行,但是如果对自己要求更高一些,应该这样写
            using(MemcachedClient client = new MemcachedClient())
            {
                // Store the record
                client.Store(StoreMode.Set, "currentTime", DateTime.Now.ToString());
                // Retrieve the value
                string value = client.Get("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();
      ///
      /// Retrieve cached item
      ///
      /// Type of cached item
      /// Name of cached item
      /// Cached item as type
      public static T Get(string key) where T : class
      {
            try
            {
                return (T) Cache.Get(key);
            }
            catch
            {
                return null;
            }
      }
      ///
      /// Insert value into the cache using
      /// appropriate name/value pairs
      ///
      /// Type of cached item
      /// Item to be cached
      /// Name of item
      /// Duration of the cache.
      public static void Add(T objectToCache, string key, int cacheDuration) where T : class
      {
            Cache.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration));
      }
      ///
      /// Insert value into the cache using
      /// appropriate name/value pairs
      ///
      /// Item to be cached
      /// Name of item
      /// Duration of the cache.
      public static void Add(object objectToCache, string key, int cacheDuration)
      {
            Cache.Store(StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes(cacheDuration));
      }
      ///
      /// Remove item from cache
      ///
      /// Name of cached item
      public static void Remove(string key)
      {
            Cache.Remove(key);
      }
      ///
      /// Clears all stored objects from memory.
      ///
      public static void ClearAll()
      {
            Cache.FlushAll();
      }
      ///
      /// Check for item in cache
      ///
      /// Name of cached item
      /// A boolean if the object exists
      public static bool Exists(string key)
      {
            return Cache.Get(key) != null;
      }
    }
}  

  

  

  

  

  



附件:http://down.运维网.com/data/2367369

页: [1]
查看完整版本: #Memcached系列#(6)使用Enyim.Caching访问Memcached的一个C#控制台程序