那點所謂的分布式——memcache

來源:互聯網
上載者:User

 

   這篇開始決定把系列文章的名字改掉,想了個好名字,反正不是玩單機版的就行了。

    好了,這篇我們看看一種非持久化的快取服務器memcache,說到緩衝本能反映就是cache,session什麼的,是的,可以說這

些都是基於.net進程的,通俗點也就做不了多機器的共用,典型的一個就是SSO。

 

一: 安裝

     memcahce像redis,mongodb一樣都需要開啟他們自己的服務端,我們下載Memcached_1.2.5.zip,然後放到C盤,修改檔案

名為memcached。

1:install

     install可以說是萬能通用命令,首先我們轉到memcached目錄,然後 memcached.exe -d install 即可。

        

2:start

    現在我們只要啟動start即可,要注意的就是memecache預設的連接埠是11211,當然我也不想重新指定連接埠了。

        

3:stop,uninstall

     這兩個就不了,一個是停止,一個是卸載,反正都是萬能通用命令。

 

二:驅動程式

   memcache的伺服器我們就已經開啟好了,由於在公司最近一直都在用php,算了還是用C#驅動吧,誰讓這是.net

社區呢,下載C#驅動,既然是快取服務器,只要有基本的CURD,我想應該就差不多了。

 1 using System; 2 using System.Collections.Generic; 3  4 namespace BeIT.MemCached 5 { 6     class Example 7     { 8         public static void Main(string[] args) 9         {10             //通過設定檔初始化memcache執行個體11             MemcachedClient cache = MemcachedClient.GetInstance("MyConfigFileCache");12 13             //編輯(可以類比session操作,緩衝20分鐘)14             cache.Set("name", "一線碼農", DateTime.Now.AddMinutes(20));15 16             //擷取17             var result = cache.Get("name");18 19             Console.WriteLine("擷取name的快取資料為: " + result);20 21             //刪除22             cache.Delete("name");23 24             Console.WriteLine("\n成功刪除cache中name的資料");25 26             result = cache.Get("name");27 28             Console.WriteLine("\n再次擷取cache中name的資料為:" + (result ?? "null") + "\n");29 30             //查看下memecahce的運行情況31             foreach (KeyValuePair<string, Dictionary<string, string>> host in cache.Status())32             {33                 Console.Out.WriteLine("Host: " + host.Key);34                 foreach (KeyValuePair<string, string> item in host.Value)35                 {36                     Console.Out.WriteLine("\t" + item.Key + ": " + item.Value);37                 }38                 Console.Out.WriteLine();39             }40 41             Console.Read();42         }43     }44 }

我們再定義下設定檔,既然memcache可以用於分布式,那就避免不了將cache分攤到幾台伺服器上去,可以看到,下面的

配置也是非常簡單的,當然分配的法則自然是memcache自身的演算法決定的,最後別忘了在另一台伺服器上開放一個連接埠就它

就行了。

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <section name="beitmemcached" type="System.Configuration.NameValueSectionHandler" />  </configSections>  <appSettings>  </appSettings>  <beitmemcached>    <add key="MyConfigFileCache" value="127.0.0.1:11211" />    <!--<add key="MyConfigFileCache" value="127.0.0.1:11211,127.0.0.1:8888" />-->  </beitmemcached></configuration>

 

下面是打包程式:BeITMemcached ,也可以到codegoogle去下載。

 

聯繫我們

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