C# 操作Memcached

C# 操作Memcached

环境 c# vs2013 ,要在nuget里引用(安装) EnyimMemcached ,下面是以调用阿里云的ocs(memcached)服务为例

using System;
using Enyim.Caching;


namespace MemcacheTest
{
    /// <summary>
    /// MemcachedClient 帮组类 对外提供接口方法
    /// </summary>
    public class MemcachedHelper
    {
        /// <summary>
        /// 定义一个静态MemcachedClient客户端,它随类一起加载,所有对象共用
        /// </summary>
        private static MemcachedClient mclient;
        /// <summary>
        /// 静态构造函数,初始化Memcached客户端
        /// </summary>
        static MemcachedHelper()
        {
            mclient = MemCached.getInstance();
        }
        /// <summary>
        /// 向Memcached缓存中添加一条数据
        /// </summary>
        /// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expiry">过期时间</param>
        /// <returns>返回是否添加成功</returns>
        public static bool SetValue(string groupName,string key, object value, DateTime expiry)
        {
            key = groupName + "-" + key;
            return mclient.Store(Enyim.Caching.Memcached.StoreMode.Set,key, value, expiry);
        }
        /// <summary>
        /// 向Memcached缓存中添加一条数据 默认超时24小时
        /// </summary>
        /// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetValue(string groupName, string key, object value)
        {
            key = groupName + "-" + key;
            return mclient.Store(Enyim.Caching.Memcached.StoreMode.Set, key, value, DateTime.Now.AddHours(24));
        }
        /// <summary>
        /// 通过key 来得到一个对象
        /// </summary>
        /// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
        /// <param name="key">键</param>
        /// <returns>对象</returns>
        public static object GetValue(string groupName, string key)
        {
            key = groupName + "-" + key;
            return mclient.Get(key);
        }
        /// <summary>
        /// 通过key 来得到一个对象(前类型)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static T GetValue<T>(string groupName, string key)
        {
            key = groupName + "-" + key;
            return mclient.Get<T>(key);
        }
        /// <summary>
        /// 清除指定key的cache
        /// </summary>
        /// <param name="groupName">组名,用来区分不同的服务或应用场景</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static bool Remove(string groupName, string key)
        {
            key = groupName + "-" + key;
            return mclient.Remove(key);
        }
        /// <summary>
        /// 清除所有cache
        /// </summary>
        public static void RemoveAll()
        {
             mclient.FlushAll();
        }
    }
}


using System.Net;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;


namespace MemcacheTest
{
    /// <summary>
    /// MemcachedClient 配置类
    /// </summary>
    public sealed class MemCached
    {
        private static MemcachedClient MemClient;
        static readonly object padlock = new object();
        //线程安全的单例模式
        public static MemcachedClient getInstance()
        {
            if (MemClient == null)
            {
                lock (padlock)
                {
                    if (MemClient == null)
                    {
                        MemClientInit();
                    }
                }
            }
            return MemClient;
        }
        static void MemClientInit()
        {
            //初始化缓存
            MemcachedClientConfiguration memConfig = new MemcachedClientConfiguration();
            IPAddress newaddress = IPAddress.Parse(Dns.GetHostEntry("XXXXXXXXXX.m.cnhzalicm10pub001.ocs.aliyuncs.com").AddressList[0].ToString()); //xxxx替换为ocs控制台上的“内网地址”
            IPEndPoint ipEndPoint = new IPEndPoint(newaddress, 11211);
            // 配置文件 - ip
            memConfig.Servers.Add(ipEndPoint);
            // 配置文件 - 协议
            memConfig.Protocol = MemcachedProtocol.Binary;
            // 配置文件-权限,如果使用了免密码功能,则无需设置userName和password
            memConfig.Authentication.Type = typeof(PlainTextAuthenticator);
            memConfig.Authentication.Parameters["zone"] = "";
            memConfig.Authentication.Parameters["userName"] = "XXXXXXXXXXXXXXXXX";
            memConfig.Authentication.Parameters["password"] = "XXXXXXXXXX";
            //下面请根据实例的最大连接数进行设置
            memConfig.SocketPool.MinPoolSize = 5;
            memConfig.SocketPool.MaxPoolSize = 200;
            MemClient = new MemcachedClient(memConfig);
        }
    }
}


程序调用
 MemcachedHelper.SetValue(groupName,strKey, strValue, DateTime.Now.AddMinutes(5));

var restr = MemcachedHelper.GetValue(groupName,strKey);

var obj = MemcachedHelper.GetValue<Product>(groupName,"p001");




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值