memcache/memcached/memcachedb 配置、安裝

memcache/memcached/memcachedb 配置、安裝

當你聽到memcache與memcached時把它當做是一個東東就好了,儘管它們存在區別,但是這並不影響你對它們的運用及理解。

“Memcache”它是一個自由和開放源代碼、高性能、分配的內存對象緩存系統,即該系統名稱爲“Memcache”;

“Memcached”它是該系統的主程序文件,以守護程序方式運行於一個或多個服務器中(分佈式),隨時接受客戶端的連接操作,使用共享內存存取數據;

“Memcachedb”它是新浪2007年的項目,在Memcached的基礎上開發出來了,它與Memcache不同的是它提供了數據持久化存儲

 

首先,我們需要下載一個memcached安裝程序,memcached版本很多,開源的東西我們一定要找一個持續更新的版本,很簡單,有團隊在維護升級。

我選擇的版本是:beitmemcached,項目地址:http://code.google.com/p/beitmemcached/  注:此鏈結爲windows下memcached文程序安裝文件及示例。

圖中的兩個文件分別爲:上面的是客戶端調用示例項目文件、下面的文件是Memcached主程序安裝文件

 

然後,將memcached主程序文件安裝到服務器上。

Windows下安裝:

1.將上圖中Memcached 1.2.5.zip解壓縮到 D:\program files\memcached目錄下(此目錄自行定義)。

2.Ctrl+R,輸入cmd,打開命令行窗口,轉到D:\program files\memcached目錄下。

3.memcached.exe -d install

4.memcached.exe -d start

如果你要卸載,執行下面的命令:

1.memcached.exe -d stop

2.memcached.exe -d uninstall

Linux(CentOS 5.x)下安裝:

1. yum install gcc

2. cd /tmp

3. wget http://www.monkey.org/~provos/libevent-2.0.4-alpha.tar.gz   注:memcached 用到了 libevent 這個庫用於 Socket 的處理,所以 還需要安裝 libevent

4. tar zxvf libevent-2.0.4-alpha.tar.gz

5. cd libevent-2.0.4-alpha

6. ./configure -prefix=/usr/local/libevent

7. make

8. make install

9. cd ~

10. cd /tmp

11. http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

12. tar zxvf memcached-1.4.5.tar.gz

13. cd memcached-1.4.5

14. ./configure -prefix=/usr/local/memcached --with-libevent=/usr/local/libevent    注:安裝memcached時需要指定libevent的安裝位置

15. make

16. make install

17. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/libevent/lib   注:將libevent的lib目錄加入LD_LIBRARY_PATH裏

18. vi /etc/sysconfig/iptables 

19. 將下面這行加入進去

-A RH-Firewall-l-INPUT -p tcp -m tcp --dport 11211 -j ACCEPT  注:將memcached加入到防火牆允許訪問規則中

20. service iptables restart  注:防火牆重啓

21. /usr/local/memcached/bin/memcached -d   注:啓動memcached

 

memcached啓動參數描述:

-d :啓動一個守護進程,

-m:分配給Memcache使用的內存數量,單位是MB,默認是64MB,

-u :運行Memcache的用戶

-l  :監聽的服務器IP地址

-p :設置Memcache監聽的端口,默認是11211    注:-p(p爲小寫)

-c :設置最大併發連接數,默認是1024

-P :設置保存Memcache的pid文件   注:-P(P爲大寫)

如果要結束Memcache進程,執行:kill cat pid文件路徑

 

無論是在windows下還是在linux下安裝都非常簡單,使用起來也很簡單。

如何往memcached中插入數據?如何來讀取數據?示例代碼如下:

using System; using System.Collections.Generic; namespace BeIT.MemCached { class Example { public static void Main(string[] args) { //--------------------- // Setting up a client. //--------------------- Console.Out.WriteLine("Setting up Memcached Client."); MemcachedClient.Setup("MyCache", new string[] { "localhost" }); //It is possible to have several clients with different configurations: //If it is impossible to resolve the hosts, this method will throw an exception. try { MemcachedClient.Setup("MyOtherCache", new string[]{ "server1.example.com:12345", "server2.example.com:12345"}); } catch (Exception e) { Console.WriteLine(e.Message); } //Get the instance we just set up so we can use it. You can either store this reference yourself in //some field, or fetch it every time you need it, it doesn't really matter. MemcachedClient cache = MemcachedClient.GetInstance("MyCache"); //It is also possible to set up clients in the standard config file. Check the section "beitmemcached" //in the App.config file in this project and you will see that a client called "MyConfigFileCache" is defined. MemcachedClient configFileCache = MemcachedClient.GetInstance("MyConfigFileCache"); //Change client settings to values other than the default like this: cache.SendReceiveTimeout = 5000; cache.ConnectTimeout = 5000; cache.MinPoolSize = 1; cache.MaxPoolSize = 5; //---------------- // Using a client. //---------------- //Set some items Console.Out.WriteLine("Storing some items."); cache.Set("mystring", "The quick brown fox jumped over the lazy dog."); cache.Set("myarray", new string[]{"This is the first string.", "This is the second string."}); cache.Set("myinteger", 4711); cache.Set("mydate", new DateTime(2008, 02, 23)); //Use custom hash cache.Set("secondstring", "Flygande b鋍kasiner s鰇a hwila p?mjuka tufvor", 4711); //Get a string string str = cache.Get("mystring") as string; if (str != null) { Console.Out.WriteLine("Fetched item with key: mystring, value: " + str); } //Get an object string[] array = cache.Get("myarray") as string[]; if (array != null) { Console.Out.WriteLine("Fetched items with key: myarray, value 1: " + array[0] + ", value 2: " + array[1]); } //Get several values at once object[] result = cache.Get(new string[]{"myinteger", "mydate"}); if (result[0] != null && result[0] is int) { Console.Out.WriteLine("Fetched item with key: myinteger, value: " + (int)result[0]); } if (result[1] != null && result[1] is DateTime) { Console.Out.WriteLine("Fetched item with key: mydate, value: " + (DateTime)result[1]); } str = cache.Get("secondstring", 4711) as string; if (str != null) { Console.Out.WriteLine("Fetched item with key and custom hash: secondstring, value: " + str); } //Set a counter Console.Out.WriteLine("Setting an item for incrementing and decrementing."); cache.SetCounter("mycounter", 9000); ulong? counter = cache.GetCounter("mycounter"); if (counter.HasValue) { Console.Out.WriteLine("Fetched mycounter, value: " + counter.Value); } //Increment the counter counter = cache.Increment("mycounter", 1); if (counter.HasValue) { Console.Out.WriteLine("Incremented mycounter with 1, new value: " + counter.Value); } //Decrement the counter counter = cache.Decrement("mycounter", 9000); if (counter.HasValue) { Console.Out.WriteLine("Decremented mycounter with 9000, new value: " + counter.Value); } //Append and prepend Console.Out.WriteLine("Storing bar for append/prepend"); cache.Set("foo", "bar"); Console.Out.WriteLine("Appending baz"); cache.Append("foo", " baz"); Console.Out.WriteLine("Prepending foo"); cache.Prepend("foo", "foo "); Console.Out.WriteLine("New value: " + cache.Get("foo")); //Cas cache.Delete("castest"); Console.Out.WriteLine("Trying to CAS non-existant key castest: " + cache.CheckAndSet("castest", "a", 0)); Console.Out.WriteLine("Setting value for key: castest, value: a"); cache.Set("castest", "a"); Console.Out.WriteLine("Trying to CAS key castest with the wrong unique: " + cache.CheckAndSet("castest", "a", 0)); ulong unique; cache.Gets("castest", out unique); Console.Out.WriteLine("Getting cas unique for key castest: " + unique); Console.Out.WriteLine("Trying to CAS again with the above unique: " + cache.CheckAndSet("castest", "b", unique)); string value = cache.Gets("castest", out unique) as string; Console.Out.WriteLine("New value: " + value + ", new unique: " + unique); Console.Out.WriteLine("Displaying the socketpool status:"); foreach (KeyValuePair<string, Dictionary<string, string>> host in cache.Status()) { Console.Out.WriteLine("Host: " + host.Key); foreach (KeyValuePair<string, string> item in host.Value) { Console.Out.WriteLine("\t" + item.Key + ": " + item.Value); } Console.Out.WriteLine(); } Console.Out.WriteLine(); Console.Out.WriteLine("Finished. Press enter to exit."); Console.In.ReadLine(); } } }

 

注:memcached是以KEY-VALUE的方式進行數據存儲的,KEY的大小限制:Key(max)<=250個字符;VALUE在存儲時有限制:Value(max)<= 1M;memcached默認過期時間:ExpiresTime(max)= 30(days)。

發佈了0 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章