一、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. ios7 上 UIActivity 用的image

    在ios8 上UIActivityCategoryShare类型的UIActivity的图标支持彩色图片了,但是在ios7上不行,ios8上的 UIActivityCategoryAction类型也不 ...

  2. QListWidget

    1.失去焦点背景颜色,代码设置全选的时候,背景会是白色,需要设置失去焦点背景颜色.(设置焦点,会出现白转化成设置背景色,效果不好) QPalette p; p.setColor(QPalette::I ...

  3. Storm集成Kafka应用的开发

    我们知道storm的作用主要是进行流式计算,对于源源不断的均匀数据流流入处理是非常有效的,而现实生活中大部分场景并不是均匀的数据流,而是时而多时而少的数据流入,这种情况下显然用批量处理是不合适的,如果 ...

  4. 配置IIS,Apache,PHP过程中遇到的一些问题

    下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...

  5. SAP系统更改小数点显示问题

    在SAP系统中会出现小数点显示的问题,比如123.12,正常情况下是这样显示,但SAP系统是德国的出的系统,德国的书写数字的习惯是将小数点“.”写成“,”逗号,显示为:123,12 这个问题可以使用事 ...

  6. 20145213《Java程序设计》实验二Java面向对象程序设计实验报告

    20145213<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装,继承,多态 初步掌握UML建模 熟悉S.O. ...

  7. supersr--NSURLSessionConfiguration-下载进度

    ////  ViewController.m//  下载进度 // //  Created by Super on 14/7/4. //  Copyright (c) 2014年 iOS. All r ...

  8. September 22nd 2016 Week 39th Thursday

    Things won are done, the soul of joy lies in the doing. 得到即是完结,快乐的精髓在于过程. Things won are done, thing ...

  9. Maven管理

    来自: http://www.cnblogs.com/bigtall/archive/2011/03/23/1993253.html.

  10. checkbox复选框全选

    HTML: <input type="checkbox" class="all"> <input type="checkbox&qu ...