Memcached分布式Cache的簡單測試
服務器端安裝:
1. 下載win32 版本
memcached-1.2.1-win32.zip
下載memcache的windows穩定版,解壓放在c:/memcached
2. 在終端下輸入 'c:/memcached/memcached.exe -d install' 安裝
3. 在終端下輸入 'c:/memcached/memcached.exe -d start' 啟動
NOTE: 以後memcached將作為windows的一個服務每次開機時自動啟動。
客戶端測試:
下載java client端jar包
測試代碼如下:
/**
* MemCached Java client
* Copyright (c) 2007 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author greg whalin <greg@meetup.com>
* @version 1.5.2
*/
package com.danga.MemCached.test;
import com.danga.MemCached.*;
import org.apache.log4j.*;
public class Test {
public static void main(String[] args) {
// memcached should be running on port 11211 but NOT on 11212
BasicConfigurator.configure();
//String[] servers = { "192.168.1.14:11211", "192.168.1.14:11211" };
String[] servers = { "127.0.0.1:11211" };
SockIOPool pool = SockIOPool.getInstance();
pool.setServers( servers );
pool.setFailover( true );
pool.setInitConn( 10 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaintSleep( 30 );
pool.setNagle( false );
pool.setSocketTO( 3000 );
pool.setAliveCheck( true );
pool.initialize();
MemCachedClient memCachedClient = new MemCachedClient();
boolean success1 = memCachedClient.set( "test", "aspboy2009!" );
System.out.println( (String)memCachedClient.get("test") );
boolean success2 = memCachedClient.set( "k1", "k1value!" );
System.out.println( (String)memCachedClient.get("k1") );
boolean success3 = memCachedClient.set( "k2", "k2value!" );
System.out.println( (String)memCachedClient.get("k2") );
memCachedClient.statsSlabs();
memCachedClient.stats();
//memCachedClient.delete("k1");
//memCachedClient.replace("k3", "kkkkk333");
}
}
最後更新:2017-04-02 03:42:36