1 服务端
1)下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached
2)在cmd下输入 c:\memcached\memcached.exe -d install 安装
3)再输入: c:\memcached\memcached.exe -d start 启动。
2 客户端
Memcached客户端框架 xmemcached
maven包地址:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>1.4.1</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
spring配置文件:
<bean name="memcachedClient"
class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean">
<property name="servers">
<value>192.168.1.132:12000 192.168.1.132:12001</value>
</property>
<property name="weights">
<list>
<value>1</value>
<value>1</value>
</list>
</property>
<property name="connectionPoolSize" value="10"></property>
<property name="opTimeout" value="2000"></property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>
</property>
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
</bean>
<bean name="cache" class="com.nbtv.cache.impl.MemcachedImpl">
<property name="memcachedClient">
<ref local="memcachedClient"/>
</property>
</bean>
MemcachedImpl.java类:
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.nbtv.cache.ICache;
public class MemcachedImpl implements ICache {
private static Log log = LogFactory.getLog(MemcachedImpl.class);
private MemcachedClient memcachedClient = null;
public void setMemcachedClient(MemcachedClient memcachedClient) {
this.memcachedClient = memcachedClient;
}
public boolean add(String key, Object value) {
try {
return memcachedClient.add(key, DEFAULT_EXP_TIME, value);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method add(String key, Object value) exception[key=")
.append(key).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
public boolean add(String key, int exp, Object value) {
try {
return memcachedClient.add(key, exp, value);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method add(key, exp, value) exception[key=")
.append(key).append(", exp=").append(exp).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
public boolean set(String key, Object value) {
try {
return memcachedClient.set(key, DEFAULT_EXP_TIME, value);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method set(String key, Object value) exception[key=")
.append(key).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
public boolean set(String key, int exp, Object value) {
try {
return memcachedClient.set(key, exp, value);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method set(key, exp, value) exception[key=")
.append(key).append(", exp=").append(exp).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
/**
* 乐观锁方式的更新
* @param key
* @param value
* @return
*/
public boolean cas(String key, Object value, long cas) {
try {
return memcachedClient.cas(key, DEFAULT_EXP_TIME, value, cas);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method cas(String key, Object value) exception[key=")
.append(key).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
/**
* 乐观锁方式的更新
* @param key
* @param exp
* @param value
* @return
*/
public boolean cas(String key, int exp, Object value, long cas) {
try {
return memcachedClient.cas(key, exp, value, cas);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method cas(key, exp, value) exception[key=")
.append(key).append(", exp=").append(exp).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
public boolean append(String key, Object value) {
try {
return memcachedClient.append(key, value);
} catch (Exception ex) {
String errorMsg = new StringBuffer().append("call method append(key, value) exception[key=")
.append(key).append(", value=").append(value).append("]").toString();
log.error(errorMsg, ex);
return false;
}
}
public boolean delete(String key) {
try {
return memcachedClient.delete(key);
} catch(Exception ex) {
log.error(new StringBuffer("call method delete(key) exception[key=").append(key).append("]").toString(), ex);
return false;
}
}
public Object get(String key) {
try {
return memcachedClient.get(key);
} catch(Exception ex) {
log.error(new StringBuffer("call method get(key) exception[key=").append(key).append("]").toString(), ex);
return null;
}
}
public void flushAll() {
try {
memcachedClient.flushAll();
} catch(Exception ex) {
log.error("call method flushAll() exception", ex);
}
}
/**
*
* @param host 192.168.0.121:12000
*/
public void flushAll(String host) {
try {
memcachedClient.flushAll(AddrUtil.getOneAddress(host));
} catch(Exception ex) {
log.error(new StringBuffer("call method flushAll(String host) exception[host=").append(host).append("]").toString(), ex);
}
}
@Override
public com.nbtv.cache.GetsResponse gets(String key) {
try {
GetsResponse res =memcachedClient.gets(key);
return new com.nbtv.cache.GetsResponse(res.getCas(),res.getValue());
} catch(Exception ex) {
log.error(new StringBuffer("call method gets(key) exception[key=").append(key).append("]").toString(), ex);
return null;
}
}
@Override
public boolean existValue(String key, Object value) {
try {
Object ret = memcachedClient.get(key);
return value.equals(ret);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// public boolean touch(String key, int exp){
// try {
// return memcachedClient.touch(key, exp);
// } catch (Exception ex) {
// String errorMsg = new StringBuffer().append("call method touch(key, exp) exception[key=")
// .append(key).append(", exp=").append(exp).append("]").toString();
// log.error(errorMsg, ex);
// return false;
// }
// }
public long incr(String key, long delta) throws Exception{
// try {
return memcachedClient.incr(key, delta);
// } catch (Exception ex) {
// String errorMsg = new StringBuffer().append("call method set(String key, Object value) exception[key=")
// .append(key).append(", delta=").append(delta).append("]").toString();
// log.error(errorMsg, ex);
// return
// }
}
}