程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> Memcached 緩存系統的-介紹、安裝以及應用

Memcached 緩存系統的-介紹、安裝以及應用

編輯:關於C#
 

一. memcached 是什麼?
memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
memcached是一個高性能的、分布式內存對象緩存系統,應用廣泛。通過緩存數據庫查詢結果,減少數據庫訪問次數,以提高動態Web應用的速度、 提高可擴展性。
它可以應對任意多個連接,使用非阻塞的網絡IO。由於它的工作機制是在內存中開辟一塊空間,然後建立一個HashTable,Memcached自管理這些HashTable。還使用內置的內存塊分配和哈希表算法,確保虛擬內存不會過來搗亂。
Memcached 官方網站:http://www.danga.com/memcached

二. memcached 的安裝:
注:memcached 用到了libevent這個庫用於Socket的處理,所以還需要安裝libevent.官網:http://www.monkey.org/~provos/libevent/
1. 先安裝libevent:
[root@localhost software]# tar zxvf libevent-1.4.11-stable.tar.gz
[root@localhost libevent-1.4.11-stable]# ./configure –prefix=/usr
[root@localhost libevent-1.4.11-stable]# make
[root@localhost libevent-1.4.11-stable]# make install

2. 測試libevent是否安裝成功
[root@localhost libevent-1.4.11-stable]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 07-10 13:10 libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
-rwxr-xr-x 1 root root 31596 2007-01-07 libevent-1.1a.so.1.0.2
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root root 308088 07-21 03:33 libevent-1.4.so.2.1.3
-rw-r--r-- 1 root root 394474 07-21 03:33 libevent.a
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root root 109490 07-21 03:33 libevent_core-1.4.so.2.1.3
-rw-r--r-- 1 root root 148742 07-21 03:33 libevent_core.a
-rwxr-xr-x 1 root root 866 07-21 03:33 libevent_core.la
lrwxrwxrwx 1 root root 26 07-21 03:33 libevent_core.so -> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 246870 07-21 03:33 libevent_extra-1.4.so.2.1.3
-rw-r--r-- 1 root root 307370 07-21 03:33 libevent_extra.a
-rwxr-xr-x 1 root root 873 07-21 03:33 libevent_extra.la
lrwxrwxrwx 1 root root 27 07-21 03:33 libevent_extra.so -> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 831 07-21 03:33 libevent.la
lrwxrwxrwx 1 root root 21 07-21 03:33 libevent.so -> libevent-1.4.so.2.1.3

安裝OK。

3. 安裝memcached,同時需要安裝中指定libevent的安裝位置
[root@localhost software]# tar zxvf memcached-1.4.0.tar.gz
[root@localhost memcached-1.4.0]# ./configure –with-libevent=/usr
[root@localhost memcached-1.4.0]# make
[root@localhost memcached-1.4.0]# make intall

4. 測試是否成功安裝memcached
[root@localhost memcached-1.4.0]# ls -al /usr/local/bin | grep memcached
-rwxr-xr-x 1 root root 188225 07-21 03:35 memcached

安裝OK。

三. 如何啟動 memcached 服務:
只需要啟動一個 memcached 監護進程,監護進程不需要配置文件,只要在命令行裡面加三四個參數就可以了:
[root@localhost bin]# memcached -d -m 100 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
-d: (run as a daemon) 選項是啟動一個守護進程
-m:(max memory to use for items in megabytes (default: 64 MB))是分配給Memcache使用的內存數量,單位是MB,我這裡是100MB,
-u:(assume identity of <username> (only when run as root))是運行Memcache的用戶,我這裡是root,
-l:(interface to listen on)是監聽的服務器IP地址,如果有多個地址的話,這裡指定了服務器的IP地址127.0.0.1,
-p:是設置Memcache監聽的端口,這裡設置了11211,最好是1024以上的端口,
-c:選項是最大運行的並發連接數,默認是1024,這裡設置了256,根據服務器的負載量來設定,
-P:(save PID in <file>, only used with -d option)是設置保存Memcache的pid文件,這裡是保存在 /tmp/memcached.pid

注:也可以啟動多個守護進程,不過端口不能重復。

四. 安裝 Memcached 的PHP擴展:
在PHP中使用Memcached,有兩種方式:
一種是安裝PHP的memcached擴展。該擴展是用c寫的,效率較高,需要在服務器上安裝。
另外一種則是直接使用客戶端的php-memcached-client類庫。
下面是使用PECL中Memcache的專用擴展,因為畢竟是用C寫的,效率高,而且安裝部署起來也比較方便。
1. 在 http://pecl.php.net/package/memcache 選擇相應想要下載的memcache版本。我下載的是:memcache-2.2.5.tgz 版本。
2. 安裝 memcache
[root@localhost software]# tar zxvf memcache-2.2.5.tgz
[root@localhost software]# cd memcache-2.2.5
[root@localhost memcache-2.2.5]# /usr/bin/phpize
[root@localhost memcache-2.2.5]# ./configure –enable-memcache –with-php-config=/usr/bin/php-config –with-zlib-dir
[root@localhost memcache-2.2.5]# make
[root@localhost memcache-2.2.5]# make install
這步會有類似這樣的提示:Installing shared extensions: /usr/local/php/modules
3. 把/etc/php.ini中的extension_dir = “./”修改為:extension_dir = "/usr/lib/php/modules"

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved