本站木有非理性广告和有害内容,请大度地将本站加入广告屏蔽白名单吧~~~

在Windows .NET平台下使用Memcached

:: 代码生涯 泥塘塘主 180℃ 0评论

一、Memcached的安装

这里下载合适你的memcached版本,我下载的是memcached-win64-1.4.4-14 版本。

解压后,利用以下命令进行安装:

memcached -d install   // memcached.exe -m 1024 -d install    // 指定占用内存大小 默认是64M
memcached -d start
在Windows .NET平台下使用Memcached

二、.NET使用Memcached

客户端利用的是 EnyimMemcached。
首先在App.config 或者在Web.config 下配置 Memcached服务地址。
<configuration>
  
  <configSections>
    <sectionGroup name="SDCacheProviderConfig">
      <section name="sdmemcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
  </configSections>
  
  <SDCacheProviderConfig>
    <sdmemcached>
      <servers>
        <add address="192.168.5.120" port="11211" />
        <add address="192.168.5.123" port="11211" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="200" connectionTimeout="00:00:05" deadTimeout="00:02:00" />
    </sdmemcached>
  </SDCacheProviderConfig>
 
</configuration>

针对 EnyimMemcached,我写了个帮助类MemcachedHelper,

public static class MemcachedHelper
   {

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <returns></returns>        
        public static bool Cache_Store<T>(string key, T value)
        {
            string json = JsonConvert.SerializeObject(value);
            return MemcachedProvider.Cache_Store(key, json);
        }

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <param name="timespan">timespan</param>
        /// <returns></returns>
        public static bool Cache_Store<T>(string key, T value, TimeSpan timespan)
        {
            string json = JsonConvert.SerializeObject(value);
            return MemcachedProvider.Cache_Store(key, json, timespan);
        }

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <param name="expiresAt">过期时间</param>
        /// <returns></returns>
        public static bool Cache_Store<T>(string key, T value, DateTime expiresAt)
        {
            string json = JsonConvert.SerializeObject(value);
            return MemcachedProvider.Cache_Store(key, json, expiresAt);
        }

        /// <summary>
        /// 缓存-获取
        /// </summary>
        /// <param name="key">存储键值,最好把产品名称跟上,不会重名</param>
        /// <returns>返回对象</returns>
        public static T Cache_Get<T>(string key)
        {

            object o = MemcachedProvider.Cache_Get(key);
            if (o == null)
            {
                return default(T);
            }

            return JsonConvert.DeserializeObject<T>(o.ToString());
        }

        /// <summary>
        /// 缓存-移除
        /// </summary>
        /// <param name="key">存储键值,最好把产品名称跟上,不会重名</param>
        /// <returns></returns>
        public static bool Cache_Remove(string key)
        {
            return MemcachedProvider.Cache_Remove(key);
        }


    }

MemcachedHelper 利用的类MemcachedProvider 实现如下:

public class MemcachedProvider
    {
        public static MemcachedClient _mc = null;

        static MemcachedProvider()
        {
            _mc = new MemcachedClient("SDCacheProviderConfig/sdmemcached");
        }


        #region 缓存 读 存 删

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <returns></returns>
        public static bool Cache_Store(string key, object value)
        {
            return _mc.Store(Enyim.Caching.Memcached.StoreMode.Set,key, value);
        }

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <param name="timespan">timespan</param>
        /// <returns></returns>
        public static bool Cache_Store(string key, object value, TimeSpan timespan)
        {
            return _mc.Store(Enyim.Caching.Memcached.StoreMode.Set,key, value, timespan);
        }

        /// <summary>
        /// 缓存-存储
        /// </summary>
        /// <param name="key">存储键值,输入时,最好把产品名称跟上,不会重名</param>
        /// <param name="value">存储值</param>
        /// <param name="expiresAt">过期时间</param>
        /// <returns></returns>
        public static bool Cache_Store(string key, object value, DateTime expiresAt)
        {
            return _mc.Store(Enyim.Caching.Memcached.StoreMode.Set,key, value, expiresAt);
        }

        /// <summary>
        /// 缓存-获取
        /// </summary>
        /// <param name="key">存储键值,最好把产品名称跟上,不会重名</param>
        /// <returns>返回对象</returns>
        public static object Cache_Get(string key)
        {
            return _mc.Get(key);
        }

        /// <summary>
        /// 缓存-移除
        /// </summary>
        /// <param name="key">存储键值,最好把产品名称跟上,不会重名</param>
        /// <returns></returns>
        public static bool Cache_Remove(string key)
        {
            return _mc.Remove(key);
        }

        #endregion 缓存



    }

三、源码下载

源码在这里。

转载请注明:泥塘·物语 » 在Windows .NET平台下使用Memcached

喜欢 (1)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址