码迷,mamicode.com
首页 > Web开发 > 详细

(转)Memcached 之 .NET(C#)实例分析

时间:2014-05-23 22:44:35      阅读:443      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

一:Memcached的安装

step1. 下载memcache(http://jehiah.cz/projects/memcached-win32)的windows稳定版(这里我下载了memcached 1.2.1 for Win32 binaries (Dec 23, 2006) 这个版本),解压放某个盘下面,比如在c:\memcached
step2. 在终端(也即cmd命令界面)下输入 ‘c:\memcached\memcached.exe -d install’ 安装
step3. 再输入: ‘c:\memcached\memcached.exe -d start’ 启动。

          PS: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

 

二: .NET memcached client library
       下载文件:https://sourceforge.net/projects/memcacheddotnet/
 

       将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录

       引用Memcached.ClientLibrary.dll

 

程序实例

 

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣代码
using System;
using System.Collections;
using Memcached.ClientLibrary;
using System.Text;

namespace Memcache
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                
if (Request["action"== "clear")
                    
this.clear();
                
else
                    
this.test();
            }
        }

        
public void clear()
        {
            
string[] servers = { "192.168.1.113:11211""192.168.202.128:11211" };

            
//初始化池
            SockIOPool pool = SockIOPool.GetInstance();
            pool.SetServers(servers);
            pool.InitConnections 
= 3;
            pool.MinConnections 
= 3;
            pool.MaxConnections 
= 5;
            pool.SocketConnectTimeout 
= 1000;
            pool.SocketTimeout 
= 3000;
            pool.MaintenanceSleep 
= 30;
            pool.Failover 
= true;
            pool.Nagle 
= false;
            pool.Initialize();
            MemcachedClient mc 
= new Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression 
= false;
            mc.Delete(
"cache");
            mc.Delete(
"endCache");
            Response.Write(
"清空缓存成功");
        }


        
public void test()
        {
            
//分布Memcachedf服务IP 端口
            string[] servers = { "192.168.1.113:11211""192.168.202.128:11211" };

            
//初始化池
            SockIOPool pool = SockIOPool.GetInstance();
            pool.SetServers(servers);
            pool.InitConnections 
= 3;
            pool.MinConnections 
= 3;
            pool.MaxConnections 
= 5;
            pool.SocketConnectTimeout 
= 1000;
            pool.SocketTimeout 
= 3000;
            pool.MaintenanceSleep 
= 30;
            pool.Failover 
= true;
            pool.Nagle 
= false;
            pool.Initialize();
            
//客户端实例
            MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression 
= false;
            StringBuilder sb 
= new StringBuilder();
            
//写入缓存
            sb.AppendLine("写入缓存测试:");
            sb.AppendLine(
"<br>_______________________________________<br>");
            
if (mc.KeyExists("cache"))
            {
                sb.AppendLine(
"缓存cache已存在");
            }
            
else
            {
                mc.Set(
"cache""写入缓存时间:" +DateTime.Now.ToString());
                sb.AppendLine(
"缓存已成功写入到cache");
            }
            sb.AppendLine(
"<br>_______________________________________<br>");
            sb.AppendLine(
"读取缓存内容如下:<br>");
            sb.AppendLine(mc.Get(
"cache").ToString());

            
//测试缓存过期
            sb.AppendLine("<br>_______________________________________<br>");
            
if (mc.KeyExists("endCache"))
            {
                sb.AppendLine(
"缓存endCache已存在,过期时间为:" +  mc.Get("endCache").ToString());
            }
            
else
            {
                mc.Set(
"endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1));
                sb.AppendLine(
"缓存已更新写入到endCache,写入时间:" + DateTime.Now.ToString()  +" 过期时间:" +  DateTime.Now.AddMinutes(1).ToString());
            }

            
//分析缓存状态
            Hashtable ht = mc.Stats();
            sb.AppendLine(
"<br>_______________________________________<br>");
            sb.AppendLine(
"Memcached Stats:");
            sb.AppendLine(
"<br>_______________________________________<br>");
            
foreach (DictionaryEntry de in ht)
            {
                Hashtable info 
= (Hashtable)de.Value;
                
foreach (DictionaryEntry de2 in info)
                {
                    sb.AppendLine(de2.Key.ToString() 
+ ":&nbsp;&nbsp;&nbsp;&nbsp;" + de2.Value.ToString() + "<br>");
                }
            }
            Response.Write(sb.ToString());
        }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣

 转自:http://www.cnblogs.com/Caceolod/articles/1710342.html

(转)Memcached 之 .NET(C#)实例分析,布布扣,bubuko.com

(转)Memcached 之 .NET(C#)实例分析

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/cheng5x/p/3737078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!