如何使用Enyim.Caching访问Memcached

发布时间:2021-10-21 10:45:12 作者:小新
来源:亿速云 阅读:257

这篇文章将为大家详细讲解有关如何使用Enyim.Caching访问Memcached,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

进入网址https://github.com/enyim ,可以看到如下界面,并选择“EnyimMemcached”

如何使用Enyim.Caching访问Memcached

进入如下页面,或者直接访问:https://github.com/enyim/EnyimMemcached,点击右侧的“Download ZIP”,得到文件“EnyimMemcached-master.zip”

如何使用Enyim.Caching访问Memcached

(2)将“EnyimMemcached-master.zip”解压出来,如下图

注:我们可以用“*.dll”进行搜索一下,发现并没有Enyim.Caching.dll,所以需要我们自己生成。

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

如何使用Enyim.Caching访问Memcached

在打开的过程中,我遇到了(好几次)下面的提示,以我目前的水平,还不能确切的明白 “这个提示到底会发生什么事情”,所以我将它忽略了,如果有明白的朋友,可以告诉我啊

如何使用Enyim.Caching访问Memcached

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

如何使用Enyim.Caching访问Memcached

针对上面的问题,我们可以查看右侧的“解决方案资源管理器”内各个项目的引用信息,发现

-->Enyim.Caching.Log4NetAdaper项目中log4net的引用丢失

-->MemcachedTest项目中nuit.framework、nuit.mocks的引用丢失

如何使用Enyim.Caching访问Memcached

对于这个问题,我们可以Nuget来解决一下

如何使用Enyim.Caching访问Memcached

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

如何使用Enyim.Caching访问Memcached

再用Nuget解决一下这个NUnit的问题,搜索nunit.mocks,安装一下,再按F6(生成解决方案),发现成功了。

如何使用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)新建一个“控制台应用程序”,将Enyim.Caching.dll放到项目的bin目录下

如何使用Enyim.Caching访问Memcached

LearningEnyimMemcached\bin

如何使用Enyim.Caching访问Memcached

添加Enyim.Caching的引用

如何使用Enyim.Caching访问Memcached

(5)添加一个“应用程序配置文件”,文件名是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,发现有11个错误,说是“未能找到Enyim的命名空间”,可是,我已经添加了Enyim.Caching的引用了。

如何使用Enyim.Caching访问Memcached

针对这个问题,打开项目的属性,将项目的“目标框架”(由原来的.NET Framework 4 Client Profile)改成“.NET Framework 4”,再按F6(生成解决方案),就成功了。

如何使用Enyim.Caching访问Memcached

按F5(启动调试),看到如下结果

如何使用Enyim.Caching访问Memcached

(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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Memcached 小结
  2. PHP使用memcached储存session信息

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

memcached

上一篇:怎么理解spring boot starter

下一篇:如何实现ShardingSphere jdbc集成多数据源

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》