千家信息网

如何使用Enyim.Caching访问Memcached

发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章将为大家详细讲解有关如何使用Enyim.Caching访问Memcached,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。(1)首先下载EnyimMemca
千家信息网最后更新 2024年09月21日如何使用Enyim.Caching访问Memcached

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

(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中的配置如下

            

(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        [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("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;        }    }}

关于"如何使用Enyim.Caching访问Memcached"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

文件 项目 问题 生成 方案 篇文章 解决方案 代码 更多 错误 面的 成功 作者 右侧 应用程序 文件名 版本 目录 程序 空间 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 医院内网服务器主机用价值多少 威海商友网络技术公司怎么样 梦幻国服服务器拥挤 重庆招聘的软件开发 路由器连接酒店网络安全吗 sql数据库汉子怎么用括号 防范网络安全行为的技术措施 关系数据库中用来联系 网络安全事件如何关联态势 青岛银行 软件开发部 方舟进化生存怎么选择服务器 数据库年龄怎么计算 网络安全保护知识产权保护 数据库yymmdd日期格式 软件开发公司卖软件税率 双节点服务器只启动一个节点 数据库服务器重启之后 美团外卖上面的服务器怎么删除 网络安全知识手抄报的内容 上海信息软件开发定制报价表 国际版1.17基岩版服务器 web软件开发 过程结束 阿里巴巴泉州网络技术有限公司 联想视频服务器配置使用 服务器域 bs架构应用软件开发 2020年变更调查数据库代码 网络安全宣传进校 服务器隐蔽安全 怎么换手机网页服务器
0