安裝libevent:
#tar -zxvf libevent-2.0.21-stable.tar.gz //先解壓縮
#cd libevent-2.0.21-stable //切換到libevent的目錄中
#./configure --prefix=/usr //指定安裝到/usr/目錄下
#make //編譯
#makeinstall //安裝
#ls /usr/lib | grep libevent //驗證安裝
安裝memcached:
#tar -zxvf memcached-1.4.20.tar.gz
#cd memcached-1.4.20
#./configure --prefix=/usr/local/memcached --with-libevent=/usr
#make
#make install
#ls -al /usr/local/memcached/bin //驗證安裝
PS:如果執行上述命令時出現 error: no acceptable C compiler found 請安裝GCC軟件套件: # yum install gcc
安裝成功後,即可啓動Memcached服務
# /usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.0.194 -p 11211 -c 256 -P /var/run/memcached.pid
# /usr/local/memcached/bin/memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid // 啓動memcached
# telnet localhost 11211
Trying127.0.0.1...
Connectedto localhost.
Escapecharacter is '^]'.
stats
STATpid 4402
STATuptime 1032
STATtime 1231155683
STATversion 1.2.6
STATpointer_size 32
...
END
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
a) <command name> 可以是set, add, replace。
set表示按照相應的<key>存儲該數據,沒有的時候增加,有的覆蓋。
add表示按照相應的<key>添加該數據,但是如果該<key>已經存在則會操作失敗。
replace表示按照相應的<key>替換數據,但是如果該<key>不存在則操作失敗
b) <key> 客戶端需要保存數據的key。
c) <flags> 是一個16位的無符號的整數(以十進制的方式表示)。
該標誌將和需要存儲的數據一起存儲,並在客戶端get數據時返回。
客戶可以將此標誌用做特殊用途,此標誌對服務器來說是不透明的。
d) <exptime> 過期的時間。
若爲0表示存儲的數據永遠不過時(但可被服務器算法:LRU 等替換)。
如果非0(unix時間或者距離此時的秒數),當過期後,服務器可以保證用戶得不到該數據(以服務器時間爲標準)。
e) <bytes> 需要存儲的字節數(不包含最後的”\r\n”),當用戶希望存儲空數據時,<bytes>可以爲0
f) 最後客戶端需要加上”\r\n”作爲”命令頭”的結束標誌。
<data block>\r\n
緊接着”命令頭”結束之後就要發送數據塊(即希望存儲的數據內容),最後加上”\r\n”作爲此次通訊的結束。
結果響應:reply
當以上數據發送結束之後,服務器將返回一個應答。可能有如下的情況:
a) “STORED\r\n”:表示存儲成功
b) “NOT_STORED\r\n” : 表示存儲失敗,但是該失敗不是由於錯誤。
通常這是由於”add”或者”replace”命令本身的要求所引起的,或者該項在刪除隊列之中。
自己寫了一個Memcached的系統服務腳本:
先構建/etc/init.d/memcahed這個文件,然後賦予其可執行權限:
- touch /etc/init.d/memcached
- chmod +x /etc/init.d/memcached
<span style="font-family:Courier New;">#!/bin/bash
#
# v1.0
# create by GANG.SHEN at 2014.07.21
#
# memcached - This shell script takes care of starting and stopping memcached.
#
# chkconfig: - 90 10
# description: Memcache provides fast memory based storage.
# processname: memcached
memcached_path="/usr/local/memcached/bin/memcached"
memcached_pid="/var/run/memcached.pid"
memcached_memory="256"
# Source function library.
. /etc/rc.d/init.d/functions
[ -x $memcached_path ] || exit 0
RETVAL=0
prog="memcached"
# Start daemons.
start() {
if [ -e $memcached_pid -a ! -z $memcached_pid ];then
echo $prog" already running...."
exit 1
fi
echo -n $"Starting $prog "
# Single instance for all caches
$memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid
RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
return $RETVAL
}
# Stop daemons.
stop() {
echo -n $"Stopping $prog "
killproc -d 10 $memcached_path
echo
[ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog
RETVAL=$?
return $RETVAL
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
RETVAL=$?
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $RETVAL </span>
追加爲系統服務:
- chkconfig --add memcached
- chkconfig memcached on
客戶端通過Xmemcached調用memcached:
先引入相關jar 包:
public class MemcachedClientTest {
public static void main(String[] args) {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(
AddrUtil.getAddresses("172.16.20.233:11211") );
// 設置連接池大小,即客戶端個數
// builder.setConnectionPoolSize(50);
// 使用二進制文件
// builder.setCommandFactory(new BinaryCommandFactory());
MemcachedClient memcachedClient = null;
try {
memcachedClient = builder.build();
try {
// 設置/獲取
memcachedClient.set("hello", 0, "Hello, memcached!");
System.out.println(memcachedClient.get("hello"));
// 替換
memcachedClient.replace("hello", 36000, "Hello, Xmemcached");
System.out.println(memcachedClient.get("hello"));
// 移除
// memcachedClient.delete("hello");
// System.out.println(memcachedClient.get("hello"));
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (memcachedClient != null) {
try {
memcachedClient.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
memcache和memcached的區別
Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,並通過memcached協議與守護進程通信。
PHP的客戶端目前常用的有兩個,一個是memcache,另一個是memcached,兩個客戶端只差了一個字母,這兩個的區別是什麼呢?
下載地址:http://windows.php.net/downloads/pecl/releases/memcache/3.0.8/
服務器中的memcached進程跑的是memcached服務;
實現了memcached接口的PHP擴展memcache,在PHP框架之內實現的;
實現了memcached接口的PHP擴展memcached,基於libmemcached實現;
JAVA的客戶端有java memcached、spymemcached、Xmemcached等。
官方手冊說明是最好的說明:
memcached:http://php.net/manual/zh/book.memcached.php http://pecl.php.net/package/memcached
memcache:http://php.net/manual/zh/book.memcache.php http://pecl.php.net/package/memcache
下面是PHP官網給予的相應解釋:
For those confuse about the memcached extension and the memcache extension, the short story is that both of them are clients of memcached server, and the memcached extension offer more features than the memcache extension.