MemCache安装好后,网站一直没法使用,后来查找资料,发现需要在配置文件里写几行代码,如下所示
<enyim.com>
<memcached protocol="Text">
<servers>
<!--在这里添加你的缓存服务器地址,可以是多个,IP地址以及对应的端口-->
<add address="127.0.0.1" port="11211" />
</servers>
<!--这里进行连接池大小、连接超时设置等参数的配置-->
<socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
</memcached>
</enyim.com>
下面介绍一下它的使用方式:
引用程序集
Enyim.Caching
MemcachedClient MemClient = new MemcachedClient();
/// <summary>
/// 添加缓存信息
/// </summary>
/// <param name="key"></param>
/// <param name="model"></param>
public void WriteCache(string key,UserDetailInfo model)
{
try
{
//存入MemCache 时 value必须要是字符串,网络传输都是二进制
var modelValue = JsonConvert.SerializeObject(model);
//创建
bool result=MemClient.Store(StoreMode.Set, key, modelValue,DateTime.Now.AddDays()); }
catch (Exception ex) {
logger.Error("创建memcached发生异常:"+ex);
} }
/// <summary>
/// 验证缓存是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool IsExCache(string key)
{
bool result = false;
try
{
if (MemClient.Get(key) != null)
{
result = true;
} }
catch (Exception ex)
{
logger.Error("验证memcached发生异常"+ex);
}
return result; }
/// <summary>
/// 获取缓存信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public UserDetailInfo GetWCache(string key)
{
UserDetailInfo model = new UserDetailInfo();
if (MemClient.Get(key)!=null)
{
model = JsonConvert.DeserializeObject<ADT.Entity.App.UserDetailInfo>(MemClient.Get(key).ToString());
}
return model;
} MemCache在网站中使用完毕