Memcached帮助类
一.如果用官方提供的方法,在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帮助类的更多相关文章
- Memcached通用类(基于Memcached Client Library)
分享下自己编写的Memcached通用类.欢迎大家帮忙指点下哈~ 使用的是.NET memcached client library 客户端+Memcached Providers using Sys ...
- Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用
1 修改pom.xml,添加依赖文件: <dependency> <groupId>com.whalin</groupId> <artifactId&g ...
- Memcached通用类(基于enyim.com Memcached Client)
一.如果用官方提供的方法,在web.config里面配置好了各个参数和服务器IP.如下图: <?xml version="1.0"?> <configuratio ...
- C#操作Memcached帮助类
在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...
- [转]C#操作Memcached帮助类
在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装: 服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl 接下来开始写程序, ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- .NET中MemCached使用介绍
阅读目录 1.MemCached是什么? 2.Window中MemCached安装 3.MemCached命令 4.简单示例 MemCached是什么 MemCached是一个自由开源,高性能,分布式 ...
- 谈谈Memcached与Redis
1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...
随机推荐
- YSlow的性能测试提示
Add an Expires or a Cache-Control Header tag: server There are two aspects to this rule: For static ...
- Elmah错误日志工具
Elmah错误日志工具 Exception 对于异常的处理,以前基本就是跳转到一个自定义的错误页面,觉得这样不错挺友好的.同时还是需要记录下来这些异常,才能进一步的进行修改. 怎么去记录这些错误信息呢 ...
- linux学习心得之目录树开端与/etc(图文)
linux学习心得之目录树开端与/etc(图文) linux中“一切皆文件”,学习linux一年了,在学习过程中对目录树的一点心得,分享给大家,有不对的地方敬请斧正. 不多说了,先上图: 根目录: / ...
- CC.NET-自动化发布时 Web.config 文件维护
[Hello CC.NET]自动化发布时 Web.config 文件维护 在 <[Hello CC.NET]CC.NET 实现自动化集成> 的 HellowWorld 中经实现: 1. ...
- php中empty和isset的区别
比如: $_POST[ 'test']= ' ';那么: isset($_POST[ 'test']) ===> true; empty($_POST[ 'test']) ===> tru ...
- C# .Net 使用zxing.dll生成二维码,条形码
public static string GetBarcode(string format, string value, int? width, int? height) { ...
- 解密:LL与LR解析 1(译)
解密:LL与LR解析 1 作者:Josh Haberman 翻译:杨贵福 由于GFW,我无法联系到作者,所以没有授权,瞎翻译的.原文在这里[http://blog.reverberate.org/20 ...
- 解决gsoap中文乱码的问题
解决方法一: 在main函数里初始化soap结构体后加入 soap_set_mode(&soap,SOAP_C_UTFSTRING); 这样所有的C都是utf-8的格式,只要你的win ...
- Magnum Kuernetes源码分析(二)
Kubernetes Master Stack kubernetes master的stack的resources主要分为三个部分. master wait handle wait handle主要用 ...
- this和$(this)
this指的是javascript对象而$(this)就是就jquery对象 jQuery.prototype.test=function(){ this.css("color", ...