一、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技术应用的更多相关文章

  1. Windows下MemCache多端口安装配置

    Windows下MemCache环境安装配置的文章很多,但大部分都是用的默认端口11211,如何修改默认端口.如何在一台服务器上配置多个MemCache端口?这正式本文要解决的问题. 1.从微软官网下 ...

  2. Windows下Memcache的安装与在php中使用

    memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...

  3. Windows下memcache安装使用

    Windows下Memcache安装 随着时间的推移,网上现在能找到的在 Windows下安装 Memcache 的文档大多已经过时.雪峰这里再简要介绍一下当下最新版的安装和配置方法. Memcach ...

  4. windows下memcache扩展安装和搭建

    ### windows下memcache扩展安装和搭建 背景:在做微信公众号的开发时,token的有效期为7200秒,所以需要对token进行保存,在这选择了memcache作为缓存工具 memcac ...

  5. 第二篇 Nosql讲解之windows下memcache的安装(一)

    memcached基本概念 1.Memcached是danga的一个项目,最早是LiveJournal服务的,最初为了加速LiveJournal访问速度而开发的,后来被很多大型的网站采用. 官方网站: ...

  6. windows下memcache安装及配置

    1.安装memcached服务,链接为http://i.cnblogs.com/Files.aspx, 下载解压后放在一个文件夹下,在开始搜索中输入cmd, 进入cmd黑框,cd 路径,进入memca ...

  7. windows下memcache安装

    Windows下的Memcache安装:1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:memcached2. 在终端(也即cmd命令界面)下输入 'c:memcache ...

  8. Windows下Memcache的安装及PHP扩展配置

    一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcached.exe 二.安装 WIN7 64位双击打开这个exe可能只有一个空的窗口,不能输入任何命令, ...

  9. 初生牛犊:Windows下Anti-sandboxes技术探究

    由于云端杀毒的流行,病毒基本上都会加上anti-sandboxes手段来躲避沙箱的探测,在这点上,由于一些原因,最近也一直在做这一块,所以算是总结一下吧. 一:什么是沙箱以及其他: 根据受控环境中的观 ...

随机推荐

  1. eclipse中手动导入DTD文件的方式

    DTD一般应用在应用程序中定义数据交换类型的文档,一般用在xml配置文件中,有些时候在eclipse中并不能加载一些提示,这个时候需要手动导入,导入方法如下: 1.首先根据声明的网址下载.dtd的文件 ...

  2. 8.SpringMVC参数传递

    页面参数传递到controller, 可被同名(与页面标签上的name名对应)的参数接收,用request设值,页面再取出来. 注意乱码解决办法: ①如果是get提交,则在tomcat的server. ...

  3. SqlDateTime 溢出。

    SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间 解决方法:不要怀疑自己的判断就是数据库字段里的datatime ...

  4. winrt控件

    http://www.mindscapehq.com/products/metroelements 常见的翻书,相册,图表,时间组件 demo下载地址http://assets.mindscape.c ...

  5. Git-windows安装包

    下载地址 http://192.168.168.230/sw-search-sp/soft/4e/30195/Git-2.7.2-32-bit_setup.1457942412.exe 来自为知笔记( ...

  6. linux 解压缩

    tar f 使用档案名字,这个参数是最后一个参数,后面只能接档案名 c 建立压缩档案 x 解压 t 查看内容 r 向压缩归档文件末尾追加文件 u 更新原压缩包中的文件 z 有gzip属性的 j 有bz ...

  7. Html之head部分详解

    随便打开一个网页,右击查看网页源代码,总能看到<head>-</head>封闭标签,在里面通常会包含5类标签:title.link.script.meta.style.这5类标 ...

  8. HDU 4387 Stone Game (博弈)

    题目:传送门. 题意:长度为N的格子,Alice和Bob各占了最左边以及最右边K个格子,每回合每人可以选择一个棋子往对面最近的一个空格移动.最先不能移动的人获得胜利. 题解: k=1时 很容易看出,n ...

  9. 【jquery】字符ascii码转换函数

    js 字符ascii码转换函数 字符转ascii码:用charCodeAt();ascii码砖字符:用fromCharCode(); 看一个小例子 <script> str="A ...

  10. ZendStudio如何汉化

    点击工具栏的help,看图 点击 Install New Sofaware...   看图 然后.... 在地址(12.0的版本):http://download.eclipse.org/techno ...