Linux下Memcache伺服器端的安裝
memcached現在版本為1.4.13,可以去http://memcached.org/下載下傳最新版本.
因為memcached底層通信是有libevent實作的,是以還需要去下載下傳它.下載下傳位址為http://libevent.org/.
也可以通過wget直接下載下傳
1.分别把memcached和libevent下載下傳回來,放到 /tmp 目錄下:
# cd /tmp
# wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
# wget https://github.com/downloads/libevent/libevent/libevent-2.0.19-stable.tar.gz
2.先安裝libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure --prefix=/usr
# make
# make install
3.測試libevent是否安裝成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r--r-- 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
還不錯,都安裝上了。
4.安裝memcached,同時需要安裝中指定libevent的安裝位置:
# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure --with-libevent=/usr
# make
# make install
如果中間出現報錯,請仔細檢查錯誤資訊,按照錯誤資訊來配置或者增加相應的庫或者路徑。
安裝完成後會把memcached放到 /usr/local/bin/memcached ,
5.測試是否成功安裝memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
memcached的基本設定 :
1.啟動Memcache的伺服器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid
-d選項是啟動一個守護程序,
-m是配置設定給Memcache使用的記憶體數量,機關是MB,我這裡是10MB,
-u是運作Memcache的使用者,我這裡是root,
-l是監聽的伺服器IP位址,如果有多個位址的話,我這裡指定了伺服器的IP位址192.168.0.200,
-p是設定Memcache監聽的端口,我這裡設定了12000,最好是1024以上的端口,
-c選項是最大運作的并發連接配接數,預設是1024,我這裡設定了256,按照你伺服器的負載量來設定,
-P是設定儲存Memcache的pid檔案,我這裡是儲存在 /tmp/memcached.pid,
2.如果要結束Memcache程序,執行:
# kill 'cat /tmp/memcached.pid'
利用java調用memcache:
memcache java client 下載下傳位址:https://github.com/gwhalin/Memcached-Java-Client/downloads
測試代碼如下:
package memcached;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheInvoke {
// 建立一個 memcached 用戶端對象
protected static MemCachedClient mcc = new MemCachedClient();
// 建立 memcached連接配接池
static{
// 指定memcached服務位址
String[] servers = { "192.168.58.132:12000" };
// 指定memcached伺服器負載量
Integer[] weights = { 3, 3, 2 };
// 從連接配接池擷取一個連接配接執行個體
SockIOPool pool = SockIOPool.getInstance();
// 設定伺服器和伺服器負載量
pool.setServers(servers);
pool.setWeights(weights);
// 設定一些基本的參數
// 設定初始連接配接數5 最小連接配接數 5 最大連接配接數 250
// 設定一個連接配接最大空閑時間6小時
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);
// 設定主線程睡眠時間
// 每隔30秒醒來 然後
// 開始維護 連接配接數大小
pool.setMaintSleep(30);
// 設定tcp 相關的樹形
// 關閉nagle算法
// 設定 讀取 逾時3秒鐘 set the read timeout to 3 secs
// 不設定連接配接逾時
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 開始初始化 連接配接池
pool.initialize();
// 設定壓縮模式
// 如果超過64k壓縮資料
// mcc.setCompressEnable(true);
// mcc.setCompressThreshold(64 * 1024);
}
public static void main(String args[]) {
User u1 = new User();
u1.setUserName("frady");
u1.setEmail("[email protected]");
mcc.add("fradyt", u1);
User u2 = (User) mcc.get("fradyt");
System.out.println("email=" + u2.getEmail());
u2.setEmail("[email protected]");
mcc.replace("fradyt", u2);
u2 = (User) mcc.get("fradyt");
System.out.println("email=" + u2.getEmail());
}
}
參考位址:http://www.ccvita.com/257.html