一.如果用官方提供的方法,在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="" />
<add address="127.0.0.1" port="" />
<add address="127.0.0.1" port="" />
<add address="127.0.0.1" port="" />
</servers>
<socketPool minPoolSize="" maxPoolSize="" 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 = ; 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帮助类的更多相关文章

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

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

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

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

  3. Memcached通用类(基于enyim.com Memcached Client)

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

  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、redis、RabbitMQ)

    本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...

  7. Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用

    一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...

  8. .NET中MemCached使用介绍

    阅读目录 1.MemCached是什么? 2.Window中MemCached安装 3.MemCached命令 4.简单示例 MemCached是什么 MemCached是一个自由开源,高性能,分布式 ...

  9. 谈谈Memcached与Redis

    1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...

随机推荐

  1. WPF之TreeList的实现方法1

    WPF之TreeList的实现方法(一) 做项目的时候根据需求,WPF现有的控件不能完全满足我们的需求, 很多时候我们需要对现有的控件做一下加工. 最简单的我们可能会把Tree转换成List形式有的叫 ...

  2. Easyui表单验证扩展

    简介: 使用Easyui,我们省了好多事情,不用为UI费心,只需要关注业务层面即可,下面是一些常用的验证方面的扩展,收藏下自己用 //重载$.fn.validatebox.defaults.rules ...

  3. Weka开发[2]-分类器类

    这次介绍如何利用weka里的类对数据集进行分类,要对数据集进行分类,第一步要指定数据集中哪一列做为类别,如果这一步忘记了(事实上经常会忘记)会出现“Class index is negative (n ...

  4. (Java 多线程系列)Java 线程池(Executor)

    线程池简介 线程池是指管理同一组同构工作线程的资源池,线程池是与工作队列(Work Queue)密切相关的,其中在工作队列中保存了所有等待执行的任务.工作线程(Worker Thread)的任务很简单 ...

  5. ngx-push-stream模块源码学习(五)——内存清理

    1.定时器         采用nginx自身的定时器管理机制,具体细节待学习过nginx源码后加以补充 2.channel的生成周期 (0).初始(诞生)         发布.订阅均有可能产生ch ...

  6. Linux下的IO监控与分析

    Linux下的IO监控与分析 近期要在公司内部做个Linux IO方面的培训, 整理下手头的资料给大家分享下 各种IO监视工具在Linux IO 体系结构中的位置 源自 Linux Performan ...

  7. 一款可定制的外国jQuery图表插件jqplot

    jqPlot是一个jQuery绘图插件,可以利用它制作漂亮的线状图和柱状图.jqPlot支持为图表设置各种不同的样式.提供Tooltips,数据点高亮显示等功能. 用法: 1.引入jQuery类库和相 ...

  8. android.database.CursorIndexOutOfBoundsException: Index <m> requested, with a size of <n>

    遇到这样的问题比较郁闷,造成上述问题的原因也是多种多样的. 总结一下原因: 1:得到cursor对象,没有moveToPosition()或者moveToNext()等游标操作就进行cursor.ge ...

  9. 如何隐藏Cognos Viewer

    做BI项目很多时候需要跟Portal做集成,可以将整个BI Portal放到企业门户或者只是存放一些固定的报表.由于Cognos默认运行会带出Cognos Viewer,这样就跟门户不太协调. 有几种 ...

  10. Redis 集成Spring(spring-data-redis)

    Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis,  JRedis, and RJC)进行 ...