全面解析windows下Memcache技术应用
一、Memcache介绍
Memcache 是 danga.com 的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。它可以应对任意多个连接,使用非阻塞的网络 IO 。由于它的工作机制是在内存中开辟一块空间,然后建立一个 HashTable , Memcached 自管理这些 HashTable 。Memcache是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高 可扩展性。Memcache 官方网站: http://www.danga.com/memcached。
二、Memcache安装
安装包里面会有x64和x86两个文件夹,根据操作系统选择一个打开会找到memcached.exe。这个文件不能直接双击运行安装,需要通过cmd进行安装。 
cmd命令:Memcache –d start|stop|shutdown|restart|uninstall|install 启动|停止|关闭|重启|卸载|安装。
安装步骤:1.找到文件memcache.exe路径;
2.memcache –d install安装完毕; 
3.启动服务后windows进程中可以看到memcache.exe;

4.服务器安装完成后,我们可以通过telnet到服务器测试一下(如果提示telnet命令不存在,需要去控制面板开启windows的 telnet服务功能) 例如: 10.0.27.120是安装memcache服务的ip地址,11211是默认的端口。输入stats查看参数信息。

到此memcache的安装全部完成。
三、Memcache在程序中的应用
在解决方案中新建memcache的类库,引用以下几个类 库,Commons.dll,Memcached.ClientLibrary.dll,ICSharpCode.SharpZipLib.dll。新建 MemcacheHelper.cs这个memcache的使用类。准备工作做完之后开始进入memcache的使用。
1.在web.config里面配置memcache的服务器地址
<appSettings>
<!--memcache的服务器配置,IP地址后期再改,端口号固定为11211--><add key="Memcached.ServerList" value="10.3.2.24:11211"/>
<appSettings>
2.在MemcacheHelper.cs类里面编写实现代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Memcached.ClientLibrary;
using System.Data;
using System.Configuration;
using NLP.Common;
namespace NLP.Memcache
{
public class MemcacheHelper
{
#region 全局静态对象
// 全局Socket连接池对象
private static SockIOPool sockIOPool;
public static SockIOPool CurrentPool
{
get
{
return sockIOPool;
}
}
// 全局Memcached客户端对象
private static MemcachedClient mc;
#endregion
public static bool MemcacheHelperInit()
{
try
{
// 初始化Memcached服务器列表
string[] serverList = ConfigurationManager.AppSettings["Memcached.ServerList"].Split(',');
// 初始化Socket连接池
sockIOPool = SockIOPool.GetInstance("MemPool");
sockIOPool.SetServers(serverList);
sockIOPool.Initialize();
// 初始化Memcached客户端
mc = new MemcachedClient();
mc.PoolName = "MemPool";
mc.EnableCompression = false;
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 判断pkey关键字是否在Pmc中
/// </summary>
/// <param name="pMC"></param>
/// <param name="pKey"></param>
/// <returns></returns>
public static bool IsCache(string pKey)
{
if (MemcacheHelperInit())
{
if (mc.KeyExists(pKey))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 删掉Memcache 数据
/// </summary>
/// <param name="key"> </param>
/// <returns></returns>
public static bool RemoveCache(string pKey)
{
if (MemcacheHelperInit())
{
if (!mc.KeyExists(pKey))
{
return false;
}
else
{
return mc.Delete(pKey);
}
}
else
{
return false;
}
}
/// <summary>
/// Set-新增或修改
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns>是否成功</returns>
public static bool AddCache(string key, object value)
{
if (MemcacheHelperInit())
{
if (!mc.KeyExists(key))
{
return mc.Add(key, value);
}
else
{
return mc.Set(key, value);
}
}
else
{
return false;
}
}
/// <summary>
/// Set-新增或修改
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expiry">过期时间</param>
/// <returns>是否成功</returns>
public static bool AddCache(string key, object value, DateTime expiry)
{
if (MemcacheHelperInit())
{
if (!mc.KeyExists(key))
{
return mc.Add(key, value, expiry);
}
else
{
return mc.Set(key, value, expiry);
}
}
else
{
return false;
}
}
/// <summary>
/// 根据单个key值获取Memcache 数据
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object GetCache(string key)
{
if (MemcacheHelperInit())
{
if (!mc.KeyExists(key))
{
return null;
}
else
{
return mc.Get(key);
}
}
else
{
return false;
}
}
/// <summary>
/// 根据多个key值获取Memcache 数据
/// </summary>
/// <param name="key"> </param>
/// <returns></returns>
public static Dictionary<string, object> GetCache(string[] keys)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
if (MemcacheHelperInit())
{
foreach (string key in keys)
{
object obj = mc.Get(key);
if (!dic.ContainsKey(key) && obj != null)
dic.Add(key, obj);
}
return dic;
}
else
{
return null;
}
}
}
}
3.在调用代码里面调用 MemcacheHelper.cs的memcache方法
如bool result= MemcacheHelper.AddCache("User","123456");//实现新增key位User,value值为123456的memcache
//获取memcache中value值,为json格式string ob_json = MemcacheHelper.GetCache("User").ToString();
这样就实现了memcache的新增、查询的完整应用。
全面解析windows下Memcache技术应用的更多相关文章
- Windows下MemCache多端口安装配置
Windows下MemCache环境安装配置的文章很多,但大部分都是用的默认端口11211,如何修改默认端口.如何在一台服务器上配置多个MemCache端口?这正式本文要解决的问题. 1.从微软官网下 ...
- Windows下Memcache的安装与在php中使用
memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...
- Windows下memcache安装使用
Windows下Memcache安装 随着时间的推移,网上现在能找到的在 Windows下安装 Memcache 的文档大多已经过时.雪峰这里再简要介绍一下当下最新版的安装和配置方法. Memcach ...
- windows下memcache扩展安装和搭建
### windows下memcache扩展安装和搭建 背景:在做微信公众号的开发时,token的有效期为7200秒,所以需要对token进行保存,在这选择了memcache作为缓存工具 memcac ...
- 第二篇 Nosql讲解之windows下memcache的安装(一)
memcached基本概念 1.Memcached是danga的一个项目,最早是LiveJournal服务的,最初为了加速LiveJournal访问速度而开发的,后来被很多大型的网站采用. 官方网站: ...
- windows下memcache安装及配置
1.安装memcached服务,链接为http://i.cnblogs.com/Files.aspx, 下载解压后放在一个文件夹下,在开始搜索中输入cmd, 进入cmd黑框,cd 路径,进入memca ...
- windows下memcache安装
Windows下的Memcache安装:1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:memcached2. 在终端(也即cmd命令界面)下输入 'c:memcache ...
- Windows下Memcache的安装及PHP扩展配置
一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcached.exe 二.安装 WIN7 64位双击打开这个exe可能只有一个空的窗口,不能输入任何命令, ...
- 初生牛犊:Windows下Anti-sandboxes技术探究
由于云端杀毒的流行,病毒基本上都会加上anti-sandboxes手段来躲避沙箱的探测,在这点上,由于一些原因,最近也一直在做这一块,所以算是总结一下吧. 一:什么是沙箱以及其他: 根据受控环境中的观 ...
随机推荐
- google API 点连线
这个是模拟的数据,用于测试,为了方便学习 弹出框信息都是固定的,以及操作都不是写的循环,实际开发用 setInterval 或者for 以减少冗余. <!DOCTYPE html> < ...
- java classpath批量设置shell脚本
java classpath批量设置shell脚本 注意:linux bash jar文件之间的分隔符是':' export JAR_HOME=path to directory which ...
- FileUpload控件使用初步
FileUpload控件使用初步 FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...
- Mysql 基础3
1. 逗号是个好东西2.对于多条件查询 和范围查询 的灵活运用(and 和or 的灵活运用)in 用的时候注意 补充select * from car where name like '%奥迪%' a ...
- struts2.0整合json
框架:struts2.0+hibernate2+spring 今天写代码时,需要用到json,我就直接加了两个jar包:json-lib-2.1-jdk15.jar,struts2-json-plug ...
- IOS searchBar去掉背景
修改UISearchBar的背景颜色 UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的 ...
- 项目之solr全文搜索工具之创建项目索引库
以创建项目baotao core为例 1. 在example目录下创建baotao-solr文件夹: 2. 将./solr下的solr.xml拷贝到baotao-solr目录下: 3. 在bao ...
- Google 如何修复 TrustManager 实施方式不安全的应用
引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...
- hdu1141(二进制数位,二分,打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141 题意:××公司是制造computer的,1960年它造的computer是4bit的,之后每10 ...
- [转]c++ vector 遍历方式
挺有趣的,转来记录 随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写 ...