Windows下配置使用MemCached

來源:互聯網
上載者:User
  • 工具:
    • memcached-1.2.6-win32-bin.zip     MemCached服務端程式(for win)
    • Memcached Manager             win下的MemCached管理工具
    • Memcached .NET client Library         C#/.NET memcached 用戶端dll
  1. 安裝配置MemCached服務端
    1. 下載memcached-1.2.6-win32-bin.zip ,解壓後得到memcached.exe,就是memcached的主程式了。比如我們放到MemCached伺服器下的C:\Program Files\MemCacheD下
    2. 下載安裝Memcached Manager ,通過這個來管理memcached的服務端。

      開啟MemCacheD Manager,點擊 add Server,填寫伺服器資訊。我這裡直接在本地安裝了memcached。,填完後點擊apply,成功的話右側會出現伺服器。

      點擊Add Instance添加memcached執行個體。這裡有一些配置資訊。Ip,連接埠,記憶體等等,不解釋了。點擊apply後會提示你是否現在啟動,我們這裡選是

      成功後發現右側已經有執行個體了,到此服務端配置完畢。

  2. 用戶端調用
    1. 首先需要下載Memcached .NET client Library 用戶端類庫,解壓得到一個memcacheddotnet目錄,一堆檔案。

    為測試MemCached,我們建立一個web項目。引用Memcached.ClientLibrary.dll,這個dll在memcacheddotnet\trunk\clientlib\src\clientlib\bin\2.0\Release

    1. 使用比較簡單,1個存 ,1個取。我們簡單弄兩頁面。

      加上using

using Memcached.ClientLibrary;

 

存:

 

代碼:


protected
void Page_Load(object sender, EventArgs e)

{


if (!IsPostBack)

{


string[] serverlist = { server.Text };


SockIOPool pool = SockIOPool.GetInstance();

pool.SetServers(serverlist);

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();

}

}


protected
void SetValue_Click(object sender, EventArgs e)

{


MemcachedClient mc = new
MemcachedClient();

mc.EnableCompression = false;

mc.Set(Key.Text, Value.Text);

Response.Write("<script>alert('ok')</script>");

}

 

 

取:

代碼:

protected
void GetValue_Click(object sender, EventArgs e)

{


string[] serverlist = { server.Text };


SockIOPool pool = SockIOPool.GetInstance();

pool.SetServers(serverlist);

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
MemcachedClient();

mc.EnableCompression = false;


string value = (string)mc.Get(Key.Text);

Value.Text = value;

}

 

 

看看效果:

 

點add存到memcached

 

 

點get。得到結果啦。

 

 

Ok,完畢。你也可以把用戶端的代碼再封裝一下,讓調用來的更簡單。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.