Windows7+NET4下memcached開發入門

來源:互聯網
上載者:User

環境準備:

1,下載並安裝針對windows的memcached 服務端:

http://www.splinedancer.com/memcached-win32/

下載完後安裝方法:

解壓zip到一個目錄,如 D:\DB\memcached,下面有一個檔案名稱 memcached.exe

以管理員方式啟動cmd,然後轉到解壓的目錄,本例為 D:\DB\memcached,然後執行如下命令:

//將memcached安裝為一個windows服務

memcached.exe -d install

//啟動服務,監聽 本機電腦的11211連接埠

memcached.exe  -l localhost -p 11211 -d start

 

2,下載 NMemcached.Client ------針對C#的memcached用戶端。

http://nmemcached.codeplex.com/

 

3,建立一個App(控制台,WINFORM,Web都行),然後添加對NMemcached.Client.dll 的引用

3.1 在App的App.Config(或Web.Config)中添加memcached的配置資訊,樣本如下:

<configSections>    <section name="nmemcached.client" type="NMemcached.Client.Configuration.NMemcachedConfigurationSection,NMemcached.Client"/>  </configSections>  <startup>    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  </startup>  <nmemcached.client useUTC="false">    <connectionProvider>      <add server="127.0.0.1" port="11211"></add>    </connectionProvider>  </nmemcached.client>
 
3.2 編寫範例程式碼:
 using (var client = NMemcached.Client.MemcachedClient.Create())                {                    client.ConnectionError += new System.EventHandler<NMemcached.Client.ConnectionEventArgs>(client_ConnectionError);                    client.FlushAll();                    string key = "key1";                    Student s = new Student() { Name="zhaoy",Age=111 };                    ResponseCode resp=  client.Set(key, s, DateTime.Now.AddMinutes(10));                    object keyVal = client.Get(key);                    Console.WriteLine(resp.ToString());                    Console.WriteLine(keyVal);                }//using

 

4, 命令列方式串連到memcached

4.1 如果是win7,需先開啟telnet用戶端。

4.2 開啟cmd命令列,輸入如下命令串連到memcached:

telnet 127.0.0.1 11211

斷行符號

之後就可以敲命令來和memcached互動了,樣本命令如下:

1,stats

斷行符號

//說明:列出memcached的常用資訊,比如空間用了多少,有多少快取項目等等。

2, set key1 0 0 8

斷行符號

12345678

斷行符號

//說明:寫入鍵為key1,值為12345678的資訊,key1為鍵名

命令說明參見下面的文章:

http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html

相關文章

聯繫我們

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