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

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="enyim.com">
<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
</sectionGroup>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<enyim.com>
<memcached protocol="Text">
<servers>
<add address="127.0.0.1" port="11211" />
<add address="127.0.0.1" port="11212" />
<add address="127.0.0.1" port="11213" />
<add address="127.0.0.1" port="11214" />
</servers>
<socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:05" deadTimeout="00:02:00" />
</memcached>
</enyim.com>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
</configuration>

则使用该通用类即可,组件自动调用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; /// <summary>
/// MemberHelper 的摘要说明
/// </summary>
public abstract class MemberHelper
{
public MemberHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
} #region 添加缓存
/// <summary>
/// 添加缓存(键不存在则添加,存在则替换)
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool AddCache(string key, object value)
{
using (MemcachedClient mc = new MemcachedClient())
{
return mc.Store(StoreMode.Set, key, value);
}
}
#endregion #region 添加缓存
/// <summary>
/// 添加缓存(键不存在则添加,存在则替换)
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="minutes">缓存时间(分钟)</param>
/// <returns></returns>
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 获取缓存
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key">键</param>
/// <returns>返回缓存,没有找到则返回null</returns>
public static object GetCache(string key)
{
using (MemcachedClient mc = new MemcachedClient())
{
return mc.Get(key);
}
}
#endregion #region 是否存在该缓存
/// <summary>
/// 是否存在该缓存
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static bool IsExists(string key)
{
using (MemcachedClient mc = new MemcachedClient())
{
return mc.Get(key) != null;
}
}
#endregion #region 删除缓存(如果键不存在,则返回false)
/// <summary>
/// 删除缓存(如果键不存在,则返回false)
/// </summary>
/// <param name="key">键</param>
/// <returns>成功:true失败:false</returns>
public static bool DelCache(string key)
{
using (MemcachedClient mc = new MemcachedClient())
{
return mc.Remove(key);
}
}
#endregion #region 清空缓存
/// <summary>
/// 清空缓存
/// </summary>
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; /// <summary>
/// MemberHelper 的摘要说明
/// </summary>
public abstract class MemberHelper
{
public MemberHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
} #region 创建Memcache客户端
/// <summary>
/// 创建Memcache客户端
/// </summary>
/// <param name="serverList">服务列表</param>
/// <returns></returns>
private static MemcachedClient CreateServer(List<IPEndPoint> 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 添加缓存
/// <summary>
/// 添加缓存(键不存在则添加,存在则替换)
/// </summary>
/// <param name="serverList">服务器列表</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
public static bool AddCache(List<IPEndPoint> serverList, string key, object value)
{
using (MemcachedClient mc = CreateServer(serverList))
{
return mc.Store(StoreMode.Set, key, value);
}
}
#endregion #region 添加缓存
/// <summary>
/// 添加缓存(键不存在则添加,存在则替换)
/// </summary>
/// <param name="serverList">服务器列表</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="minutes">缓存时间(分钟)</param>
/// <returns></returns>
public static bool AddCache(List<IPEndPoint> 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 获取缓存
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="serverList">服务器列表</param>
/// <param name="key">键</param>
/// <returns>返回缓存,没有找到则返回null</returns>
public static object GetCache(List<IPEndPoint> serverList,string key)
{
using (MemcachedClient mc = CreateServer(serverList))
{
return mc.Get(key);
}
}
#endregion #region 是否存在该缓存
/// <summary>
/// 是否存在该缓存
/// </summary>
/// <param name="serverList">服务器列表</param>
/// <param name="key">键</param>
/// <returns></returns>
public static bool IsExists(List<IPEndPoint> serverList,string key)
{
using (MemcachedClient mc = CreateServer(serverList))
{
return mc.Get(key) != null;
}
}
#endregion #region 删除缓存(如果键不存在,则返回false)
/// <summary>
/// 删除缓存(如果键不存在,则返回false)
/// </summary>
/// <param name="serverList">服务器列表</param>
/// <param name="key">键</param>
/// <returns>成功:true失败:false</returns>
public static bool DelCache(List<IPEndPoint> serverList, string key)
{
using (MemcachedClient mc = CreateServer(serverList))
{
return mc.Remove(key);
}
}
#endregion #region 清空缓存
/// <summary>
/// 清空缓存
/// </summary>
/// <param name="serverList">服务器列表</param>
public static void FlushCache(List<IPEndPoint> serverList)
{
using (MemcachedClient mc = CreateServer(serverList))
{
mc.FlushAll();
}
}
#endregion }

Memcached通用类(基于enyim.com Memcached Client)的更多相关文章

  1. Memcached通用类(基于Memcached Client Library)

    分享下自己编写的Memcached通用类.欢迎大家帮忙指点下哈~ 使用的是.NET memcached client library 客户端+Memcached Providers using Sys ...

  2. memcached实例(enyim.com Memcached Client)

    在上一篇文章,我们讲了,为什么要使用memched做为缓存服务器(没看的同学请点这里).下面让我们以memcached-1.2.1-win32版本的服务组件(安装后是以一个windows服务做daem ...

  3. Memcached帮助类

    一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP <?xml version="1.0"?> <configuration> ...

  4. C#操作Memcached帮助类

    在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...

  5. [转]C#操作Memcached帮助类

    在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...

  6. Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用

     1 修改pom.xml,添加依赖文件: <dependency> <groupId>com.whalin</groupId> <artifactId&g ...

  7. Key/Value之王Memcached初探:一、掀起Memcached的盖头来

    一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...

  8. 【转】Key/Value之王Memcached初探:一、掀起Memcached的盖头来

    一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...

  9. (转)实战Memcached缓存系统(7)Memcached的一些基础FAQ

    1. Memcached是什么? Memcached是分布式的内存对象缓存系统. 2. Memcached的基本数据结构是什么? Memcached是基于Key/Value对的HashMap.每一对, ...

随机推荐

  1. iOS多线程初见

    . 三种创建线程的方法 //第一种 NSThread * thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(doAc ...

  2. Android源码分析之Looper

    先来说说summary,Looper就是用来在某个线程中跑一个message loop.一个线程默认是没有message loop与之相关联的, 为了创建一个你必须在这个线程中调用Looper.pre ...

  3. 导出excel乱码问题

    今天遇到一个问题,在用C#做导出excel的时候,出现excel乱码问题.百度了下. 发现问题如下: 非中文字符编码问题. 解决方法: 把输出的excel格式设置成UTF-8即可. 更改代码: Res ...

  4. Effective Java 13 Minimize the accessibility of classes and members

    Information hiding is important for many reasons, most of which stem from the fact that it decouples ...

  5. Spring中Template模式与callback的结合使用浅析

    Spring不论是与ibatis,还是与Hibernate的结合中,都使用到了Template模式与callback技术,来达到简化代码实现的目的.Template模式也即模板模式,用于对一些不太变化 ...

  6. jQuery Validate 表单验证插件----在class属性中添加校验规则进行简单的校验

    一.下载插件包. 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.jQuery表单验证插件----添加class属性形式的校验 <!DOCTY ...

  7. jQuery Validate 表单验证插件----Validate简介,官方文档,官方下载地址

     一. jQuery Validate 插件的介绍 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆 ...

  8. android去掉顶部标题栏

    在清单文件(manifest.xml)里面实现 <application> <activity android:name="cn.ui.activity.UserRegAc ...

  9. 非常全面的讲解Hosts文件

    很奇怪有很多人不知道Hosts是什么东西.在网络病毒日渐盛行的今天,认识Hosts其实是很有用的,因为有好多的网页木马都盯上了这个文件,而在很多时候,您只需打开这个文件做一个小小的修改,就完全可以解决 ...

  10. 浅谈Java中的深拷贝和浅拷贝(转载)

    浅谈Java中的深拷贝和浅拷贝(转载) 原文链接: http://blog.csdn.net/tounaobun/article/details/8491392 假如说你想复制一个简单变量.很简单: ...