Memcached 之 .NET(C#)執行個體分析

來源:互聯網
上載者:User

標籤:

一: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

 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.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() + ":    " + de2.Value.ToString() + "<br>");                }            }            Response.Write(sb.ToString());        }    }}

 

Memcached 之 .NET(C#)執行個體分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.