it技术博客

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::


下载文件 memcached  

1.解压缩文件到e:\memcached 

2.命令行输入 e:\memcached\memcached.exe -d install' 

3.命令行输入 e:\memcached\memcached.exe -d start ,该命令启动 Memcached ,默认监听端口为 11211  

通过 memcached.exe -h 可以查看其帮助,查看memcache状态,telnet 192.168.0.98 11211。输入stats查询状态

 二.NET memcached client library(memcached的.NET客户端类库)  

下载memcached的.NET客户端类库,

下载地址:https://sourceforge.net/projects/memcacheddotnet/里面有.net1.1 和 .net2.0的两种版本, 

三.应用  

1.新建ASP.NET站点,将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll添加到web引用。    

2.新建文件如下

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Memcached.ClientLibrary;
using System.Globalization;

public partial class _Default : System.Web.UI.Page
{
public static string UserName;
public static SockIOPool pool;
public static string PName;

protected void Page_Load(object sender, EventArgs e)
{

//在应用程序启动时运行的代码
char[] separator = { ',' };
string[] serverlist = ConfigurationManager.AppSettings["Memcached.ServerList"].Split(separator);
// initialize the pool for memcache servers
try
{
//pool = SockIOPool.GetInstance();
pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);

pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 50;

pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;

pool.MaintenanceSleep = 30;
pool.Failover = true;

pool.Nagle = false;
pool.Initialize();
}
catch (Exception err)
{
throw new Exception(err.Message);
//Response.End();

//这里就可以用Log4Net记录Error啦!
}

//实例化一个客户端

MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = false;

string key = "user_info";//key值

object obj = new object();

if (mc.KeyExists(key)) //测试缓存中是否存在key的值
{

obj = mc.Get(key);
string user2 = obj.ToString();
Response.Write(user2);
mc.Delete(key); //移除缓存中key为test的项目

}
else
{

string userid = "whf";
string NowDate = DateTime.Now.ToShortTimeString();

mc.Add(key, userid+NowDate);
//mc.Set(key, userid); //存储数据到缓存服务器,这里将user这个对象缓存,key 是"user_info1"
Response.Write("没有缓存数据");
}

}
}

或者

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Memcached.ClientLibrary;
using System.Globalization;


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//实例化一个客户端

MemcachedClient mc = new MemcachedClient();
mc.EnableCompression = false;

string key = "user_info";//key值

Response.Write(Global.UserName); //Global.asax 设置的变量值

object obj = new object();

if (mc.KeyExists(key)) //测试缓存中是否存在key的值

  {

  obj = mc.Get(key);
string user2 = obj.ToString();
Response.Write("XX");

 

  }
else
{

string userid = "hf";

mc.Set(key, userid, System.DateTime.Now.AddMinutes(2)); //存储数据到缓存服务器,这里将user这个对象缓存,key 是"user_info1"
Response.Write("没有缓存数据");
}
}
}

 

posted on 2015-07-06 09:23  ie技术博客  阅读(253)  评论(0编辑  收藏  举报