Memcached帮助类

一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP

"1.0"?>

     
     "enyim.com">
      
"memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
"log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> "Text"> "127.0.0.1" port="11211" /> "127.0.0.1" port="11212" /> "127.0.0.1" port="11213" /> "127.0.0.1" port="11214" /> "10" maxPoolSize="100" connectionTimeout="00:00:05" deadTimeout="00:02:00" /> "true" targetFramework="4.0"/>

则使用该通用类即可,组件自动调用web.config里面的配置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
/// 
/// MemberHelper 的摘要说明
/// 
public abstract class MemberHelper
{
    public MemberHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region 添加缓存
    /// 
    /// 添加缓存(键不存在则添加,存在则替换)
    /// 
    /// 
    /// 
    /// 
    public static bool AddCache(string key, object value)
    {
        using (MemcachedClient mc = new MemcachedClient())
        {
            return mc.Store(StoreMode.Set, key, value);
        }
    }
    #endregion
    #region 添加缓存
    /// 
    /// 添加缓存(键不存在则添加,存在则替换)
    /// 
    /// 
    /// 
    /// 缓存时间(分钟)
    /// 
    public static bool AddCache(string key, object value, int minutes)
    { 
        using (MemcachedClient mc = new MemcachedClient())
        {
            return mc.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(minutes));
        }
    }
    #endregion
    #region 获取缓存
    /// 
    /// 获取缓存
    /// 
    /// 
    /// 返回缓存,没有找到则返回null
    public static object GetCache(string key)
    {
        using (MemcachedClient mc = new MemcachedClient())
        {
            return mc.Get(key);
        }
    }
    #endregion
    #region 是否存在该缓存
    /// 
    /// 是否存在该缓存
    /// 
    /// 
    /// 
    public static bool IsExists(string key)
    {
        using (MemcachedClient mc = new MemcachedClient())
        {
            return mc.Get(key) != null;
        }
    }
    #endregion
    #region 删除缓存(如果键不存在,则返回false)
    /// 
    /// 删除缓存(如果键不存在,则返回false)
    /// 
    /// 
    /// 成功:true失败:false
    public static bool DelCache(string key)
    {
        using (MemcachedClient mc = new MemcachedClient())
        {
            return mc.Remove(key);
        }
    }
    #endregion
    #region 清空缓存
    /// 
    /// 清空缓存
    /// 
    public static void FlushCache()
    {
        using (MemcachedClient mc = new MemcachedClient())
        {
            mc.FlushAll();
        }
    }
    #endregion
}

二.如果不想在web.config配置,那就使用下面的通用类。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
/// 
/// MemberHelper 的摘要说明
/// 
public abstract class MemberHelper
{
    public MemberHelper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region 创建Memcache客户端
    /// 
    /// 创建Memcache客户端
    /// 
    /// 服务列表
    /// 
    private static MemcachedClient CreateServer(List serverList)
    {
        MemcachedClientConfiguration config = new MemcachedClientConfiguration();//创建配置参数
        for (int i = 0; i < serverList.Count; i++)
        {
            config.Servers.Add(new System.Net.IPEndPoint(IPAddress.Parse(serverList[i].Address.ToString()), serverList[i].Port));//增加服务节点
        }
        config.Protocol = MemcachedProtocol.Text;
        config.Authentication.Type = typeof(PlainTextAuthenticator);//设置验证模式
        config.Authentication.Parameters["userName"] = "uid";//用户名参数
        config.Authentication.Parameters["password"] = "pwd";//密码参数
        MemcachedClient mac = new MemcachedClient(config);//创建客户端
        return mac;
    }
    #endregion
    #region 添加缓存
    /// 
    /// 添加缓存(键不存在则添加,存在则替换)
    /// 
    /// 服务器列表
    /// 
    /// 
    /// 
    public static bool AddCache(List serverList, string key, object value)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            return mc.Store(StoreMode.Set, key, value);
        }
    }
    #endregion
    #region 添加缓存
    /// 
    /// 添加缓存(键不存在则添加,存在则替换)
    /// 
    /// 服务器列表
    /// 
    /// 
    /// 缓存时间(分钟)
    /// 
    public static bool AddCache(List serverList,string key, object value, int minutes)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            return mc.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(minutes));
        }
    }
    #endregion
    #region 获取缓存
    /// 
    /// 获取缓存
    /// 
    /// 服务器列表
    /// 
    /// 返回缓存,没有找到则返回null
    public static object GetCache(List serverList,string key)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            return mc.Get(key);
        }
    }
    #endregion
    #region 是否存在该缓存
    /// 
    /// 是否存在该缓存
    /// 
    /// 服务器列表
    /// 
    /// 
    public static bool IsExists(List serverList,string key)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            return mc.Get(key) != null;
        }
    }
    #endregion
    #region 删除缓存(如果键不存在,则返回false)
    /// 
    /// 删除缓存(如果键不存在,则返回false)
    /// 
    /// 服务器列表
    /// 
    /// 成功:true失败:false
    public static bool DelCache(List serverList, string key)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            return mc.Remove(key);
        }
    }
    #endregion
    #region 清空缓存
    /// 
    /// 清空缓存
    /// 
    /// 服务器列表
    public static void FlushCache(List serverList)
    {
        using (MemcachedClient mc = CreateServer(serverList))
        {
            mc.FlushAll();
        }
    }
    #endregion
}

 

转载于:https://www.cnblogs.com/ideacore/p/6288934.html

你可能感兴趣的:(Memcached帮助类)