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");