POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.save.mc</groupId> <artifactId>save-mc-api</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>api</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.googlecode.xmemcached</groupId> <artifactId>xmemcached</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Class:
package com.save.mc.api; import java.io.IOException; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientBuilder; import net.rubyeye.xmemcached.XMemcachedClientBuilder; import net.rubyeye.xmemcached.exception.MemcachedException; import net.rubyeye.xmemcached.utils.AddrUtil; public class XMemcachedManager { public static MemcachedClient getInstance() { MemcachedClientBuilder builder = new XMemcachedClientBuilder( AddrUtil.getAddresses("173.1.3.102:11211")); try { return builder.build(); } catch (IOException e) { e.printStackTrace(); } throw new RuntimeException("memcached connection failed"); } public static void main(String[] args) throws TimeoutException, InterruptedException, MemcachedException { for (int i = 0; i < 100; i++) { final int b = i; new Thread() { public void run() { int a; try { a = SaveMemcachedClient.INSTANCE .saveset("save_ttt9", b); System.out.println(a); } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } } }.start(); } } }
package com.save.mc.api; import java.io.IOException; import java.math.BigDecimal; import java.net.InetSocketAddress; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.concurrent.TimeoutException; import net.rubyeye.xmemcached.CASOperation; import net.rubyeye.xmemcached.Counter; import net.rubyeye.xmemcached.GetsResponse; import net.rubyeye.xmemcached.KeyIterator; import net.rubyeye.xmemcached.KeyProvider; import net.rubyeye.xmemcached.MemcachedClient; import net.rubyeye.xmemcached.MemcachedClientCallable; import net.rubyeye.xmemcached.MemcachedClientStateListener; import net.rubyeye.xmemcached.auth.AuthInfo; import net.rubyeye.xmemcached.buffer.BufferAllocator; import net.rubyeye.xmemcached.exception.MemcachedException; import net.rubyeye.xmemcached.impl.ReconnectRequest; import net.rubyeye.xmemcached.networking.Connector; import net.rubyeye.xmemcached.transcoders.Transcoder; import net.rubyeye.xmemcached.utils.Protocol; import com.save.mc.api.bugfix.AbsCASOpt; import com.save.mc.api.bugfix.XCASOperation; import com.save.mc.api.operation.SaveAccumulateCASOperation; import com.save.mc.api.operation.SaveCASOperation; @SuppressWarnings("deprecation") public enum SaveMemcachedClient implements MemcachedClient{ INSTANCE; private MemcachedClient client = XMemcachedManager.getInstance(); public void setClient(MemcachedClient c){ this.client = c; } public MemcachedClient getClient(){ return this.client; }
/** * 此方法描述的是:安全写,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param value * - 值 */ public <T> T saveset(String key,T value) throws TimeoutException, InterruptedException, MemcachedException { return this.savecas(key, new SaveCASOperation<T>(value)); } /** * 此方法描述的是:安全写,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 * @param value * - 值 */ public <T> T saveset(String key,int exp, T value) throws TimeoutException, InterruptedException, MemcachedException { return this.savecas(key, new SaveCASOperation<T>(exp,value)); } /** * 此方法描述的是:安全累加,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param value * - 增加的步长值,支持Integer,Long,Double,Float,BigDecimal */ public <T> T saveAcc(String key,T value) throws TimeoutException, InterruptedException, MemcachedException { return this.savecas(key, new SaveAccumulateCASOperation<T>(value)); } /** * 此方法描述的是:安全累加,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 * @param value * - 增加的步长值,支持Integer,Long,Double,Float,BigDecimal */ public <T> T saveAcc(String key,int exp, T value) throws TimeoutException, InterruptedException, MemcachedException { return this.savecas(key, new SaveAccumulateCASOperation<T>(exp,value)); } /** * 此方法描述的是:安全递增++,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 */ public Integer saveIntegerInc(String key) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, 1); } /** * 此方法描述的是:安全递增++,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 * @param value * - 值 */ public Integer saveIntegerInc(String key,int exp) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, exp, 1); } /** * 此方法描述的是:安全递增++,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 */ public Long saveLongInc(String key) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, 1L); } /** * 此方法描述的是:安全递增++,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 * @param value * - 值 */ public Long saveLongInc(String key,int exp) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, exp, 1L); } /** * 此方法描述的是:安全递增++,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 */ public Double saveDoubleInc(String key) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, 1.0); } /** * 此方法描述的是:安全递增++,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 */ public Double saveDoubleInc(String key,int exp) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, exp, 1.0); } /** * 此方法描述的是:安全递增++,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 */ public Float saveFloatInc(String key) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, 1.0F); } /** * 此方法描述的是:安全递增++,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 */ public Float saveFloatInc(String key,int exp) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, exp, 1.0F); } /** * 此方法描述的是:安全递增++,永久存储 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 */ public BigDecimal saveBigDecimalInc(String key) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, new BigDecimal(1)); } /** * 此方法描述的是:安全递增++,指定存储有效期 * * @author: [email protected] * @version: 2015年1月7日 下午1:49:47 * @param key * - 关键字 * @param exp * - 存储有效时间(秒) 0-永久存储 */ public BigDecimal saveBigDecimalInc(String key,int exp) throws TimeoutException, InterruptedException, MemcachedException { return this.saveAcc(key, exp, new BigDecimal(1)); } /** * 此方法描述的是:安全写 * * @author: [email protected] * @version: 2015年1月7日 下午2:45:20 * @param key * - 关键字 * @param casOpt * - 自定义适配器 */ public <T> T savecas(String key,final AbsCASOpt<T> casOpt) throws TimeoutException, InterruptedException, MemcachedException { GetsResponse<T> response = client.gets(key); T t = casOpt.initValue(); final int exp = casOpt.initExp(); final int max = AbsCASOpt.MAX_TRIES; if (response == null){ for (int i = 0; i < max; i++) { if (client.add(key, exp, t)){ return t; }else{ try { return savecas(key, casOpt); } catch (Exception e) { e.printStackTrace(); } } } }else{ XCASOperation<T> operation = new XCASOperation<T>() { private T lastValue; public int getMaxTries() { return AbsCASOpt.MAX_TRIES; } public T getNewValue(long currentCAS, T currentValue) { lastValue = casOpt.getNewValue(currentValue); return lastValue; } public T getLastValue(){ return this.lastValue; } }; boolean casFlag = client.cas(key, exp, operation); if (casFlag){ return operation.getLastValue(); } } return t; }
public void setMergeFactor(int mergeFactor) { client.setMergeFactor(mergeFactor); } public long getConnectTimeout() { return client.getConnectTimeout(); } public void setConnectTimeout(long connectTimeout) { client.setConnectTimeout(connectTimeout); } public Connector getConnector() { return client.getConnector(); } public void setOptimizeGet(boolean optimizeGet) { client.setOptimizeGet(optimizeGet); } public void setOptimizeMergeBuffer(boolean optimizeMergeBuffer) { client.setOptimizeMergeBuffer(optimizeMergeBuffer); } public boolean isShutdown() { return client.isShutdown(); } public void addServer(String server, int port) throws IOException { client.addServer(server,port); } public void addServer(InetSocketAddress inetSocketAddress) throws IOException { client.addServer(inetSocketAddress); } public void addServer(String hostList) throws IOException { client.addServer(hostList); } public List<String> getServersDescription() { return client.getServersDescription(); } public void removeServer(String hostList) { client.removeServer(hostList); } @Deprecated public void setBufferAllocator(BufferAllocator bufferAllocator) { client.setBufferAllocator(bufferAllocator); } public <T> T get(String key, long timeout, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.get(key, timeout, transcoder); } public <T> T get(String key, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.get(key, timeout); } public <T> T get(String key, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.get(key, transcoder); } public <T> T get(String key) throws TimeoutException, InterruptedException, MemcachedException { return client.get(key); } public <T> GetsResponse<T> gets(String key, long timeout, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(key, timeout, transcoder); } public <T> GetsResponse<T> gets(String key) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(key); } public <T> GetsResponse<T> gets(String key, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(key, timeout); } @SuppressWarnings("rawtypes") public <T> GetsResponse<T> gets(String key, Transcoder transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(key, transcoder); } public <T> Map<String, T> get(Collection<String> keyCollections, long opTimeout, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.get(keyCollections,opTimeout,transcoder); } public <T> Map<String, T> get(Collection<String> keyCollections, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.get(keyCollections, transcoder); } public <T> Map<String, T> get(Collection<String> keyCollections) throws TimeoutException, InterruptedException, MemcachedException { return client.get(keyCollections); } public <T> Map<String, T> get(Collection<String> keyCollections, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.get(keyCollections, timeout); } public <T> Map<String, GetsResponse<T>> gets( Collection<String> keyCollections, long opTime, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(keyCollections, opTime, transcoder); } public <T> Map<String, GetsResponse<T>> gets( Collection<String> keyCollections) throws TimeoutException, InterruptedException, MemcachedException { return client.gets(keyCollections); } public <T> Map<String, GetsResponse<T>> gets( Collection<String> keyCollections, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.gets( keyCollections, timeout); } public <T> Map<String, GetsResponse<T>> gets( Collection<String> keyCollections, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.gets( keyCollections, transcoder); } public <T> boolean set(String key, int exp, T value, Transcoder<T> transcoder, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.set(key, exp, value, transcoder, timeout); } public boolean set(String key, int exp, Object value) throws TimeoutException, InterruptedException, MemcachedException { return client.set(key, exp, value); } public boolean set(String key, int exp, Object value, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.set(key, exp, value, timeout); } public <T> boolean set(String key, int exp, T value, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.set(key, exp, value, transcoder); } public void setWithNoReply(String key, int exp, Object value) throws InterruptedException, MemcachedException { client.setWithNoReply(key, exp, value); } public <T> void setWithNoReply(String key, int exp, T value, Transcoder<T> transcoder) throws InterruptedException, MemcachedException { client.setWithNoReply(key, exp, value, transcoder); } public <T> boolean add(String key, int exp, T value, Transcoder<T> transcoder, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.add(key, exp, value, transcoder, timeout); } public boolean add(String key, int exp, Object value) throws TimeoutException, InterruptedException, MemcachedException { return client.add(key, exp, value); } public boolean add(String key, int exp, Object value, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.add(key, exp, value, timeout); } public <T> boolean add(String key, int exp, T value, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.add(key, exp, value, transcoder); } public void addWithNoReply(String key, int exp, Object value) throws InterruptedException, MemcachedException { client.addWithNoReply(key, exp, value); } public <T> void addWithNoReply(String key, int exp, T value, Transcoder<T> transcoder) throws InterruptedException, MemcachedException { client.addWithNoReply(key, exp, value, transcoder); } public <T> boolean replace(String key, int exp, T value, Transcoder<T> transcoder, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.replace(key, exp, value, transcoder, timeout); } public boolean replace(String key, int exp, Object value) throws TimeoutException, InterruptedException, MemcachedException { return client.replace(key, exp, value); } public boolean replace(String key, int exp, Object value, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.replace(key, exp, value, timeout); } public <T> boolean replace(String key, int exp, T value, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.replace(key, exp, value, transcoder); } public void replaceWithNoReply(String key, int exp, Object value) throws InterruptedException, MemcachedException { client.replaceWithNoReply(key, exp, value); } public <T> void replaceWithNoReply(String key, int exp, T value, Transcoder<T> transcoder) throws InterruptedException, MemcachedException { client.replaceWithNoReply(key, exp, value, transcoder); } public boolean append(String key, Object value) throws TimeoutException, InterruptedException, MemcachedException { return client.append(key, value); } public boolean append(String key, Object value, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.append(key, value, timeout); } public void appendWithNoReply(String key, Object value) throws InterruptedException, MemcachedException { client.appendWithNoReply(key, value); } public boolean prepend(String key, Object value) throws TimeoutException, InterruptedException, MemcachedException { return client.prepend(key, value); } public boolean prepend(String key, Object value, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.prepend(key, value, timeout); } public void prependWithNoReply(String key, Object value) throws InterruptedException, MemcachedException { client.prependWithNoReply(key, value); } public boolean cas(String key, int exp, Object value, long cas) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, value, cas); } public <T> boolean cas(String key, int exp, T value, Transcoder<T> transcoder, long timeout, long cas) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, value, transcoder, timeout, cas); } public boolean cas(String key, int exp, Object value, long timeout, long cas) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, value, timeout, cas); } public <T> boolean cas(String key, int exp, T value, Transcoder<T> transcoder, long cas) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, value, transcoder, cas); } public <T> boolean cas(String key, int exp, CASOperation<T> operation, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, operation, transcoder); } public <T> boolean cas(String key, int exp, GetsResponse<T> getsReponse, CASOperation<T> operation, Transcoder<T> transcoder) throws TimeoutException, InterruptedException, MemcachedException { return client.cas( key, exp, getsReponse, operation, transcoder); } public <T> boolean cas(String key, int exp, GetsResponse<T> getsReponse, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { return client.cas(key, exp, getsReponse, operation); } public <T> boolean cas(String key, GetsResponse<T> getsResponse, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { return client.cas(key, getsResponse, operation); } public <T> boolean cas(String key, int exp, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { return client.cas(key,exp,operation); } public <T> boolean cas(String key, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { return client.cas(key, operation); } public <T> void casWithNoReply(String key, GetsResponse<T> getsResponse, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { client.casWithNoReply(key, getsResponse, operation); } public <T> void casWithNoReply(String key, int exp, GetsResponse<T> getsReponse, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { client.casWithNoReply(key, exp, getsReponse, operation); } public <T> void casWithNoReply(String key, int exp, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { client.casWithNoReply(key, exp, operation); } public <T> void casWithNoReply(String key, CASOperation<T> operation) throws TimeoutException, InterruptedException, MemcachedException { client.casWithNoReply(key, operation); } @Deprecated public boolean delete(String key, int time) throws TimeoutException, InterruptedException, MemcachedException { return client.delete(key, time); } public boolean delete(String key, long opTimeout) throws TimeoutException, InterruptedException, MemcachedException { return client.delete(key, opTimeout); } public boolean delete(String key, long cas, long opTimeout) throws TimeoutException, InterruptedException, MemcachedException { return client.delete(key); } public boolean touch(String key, int exp, long opTimeout) throws TimeoutException, InterruptedException, MemcachedException { return client.touch(key, exp, opTimeout); } public boolean touch(String key, int exp) throws TimeoutException, InterruptedException, MemcachedException { return client.touch(key, exp); } public <T> T getAndTouch(String key, int newExp, long opTimeout) throws TimeoutException, InterruptedException, MemcachedException { return client.getAndTouch(key, newExp, opTimeout); } public <T> T getAndTouch(String key, int newExp) throws TimeoutException, InterruptedException, MemcachedException { return client.getAndTouch(key, newExp); } public Map<InetSocketAddress, String> getVersions() throws TimeoutException, InterruptedException, MemcachedException { return client.getVersions(); } public long incr(String key, long delta) throws TimeoutException, InterruptedException, MemcachedException { return client.incr(key, delta); } public long incr(String key, long delta, long initValue) throws TimeoutException, InterruptedException, MemcachedException { return client.incr(key, delta, initValue); } public long incr(String key, long delta, long initValue, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.incr(key, delta, initValue, timeout); } public long decr(String key, long delta) throws TimeoutException, InterruptedException, MemcachedException { return client.decr(key, delta); } public long decr(String key, long delta, long initValue) throws TimeoutException, InterruptedException, MemcachedException { return client.decr(key, delta, initValue); } public long decr(String key, long delta, long initValue, long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.decr(key, delta, initValue, timeout); } public void flushAll() throws TimeoutException, InterruptedException, MemcachedException { client.flushAll(); } public void flushAllWithNoReply() throws InterruptedException, MemcachedException { client.flushAllWithNoReply(); } public void flushAll(long timeout) throws TimeoutException, InterruptedException, MemcachedException { client.flushAll(timeout); } public void flushAll(InetSocketAddress address) throws MemcachedException, InterruptedException, TimeoutException { client.flushAll(address); } public void flushAllWithNoReply(InetSocketAddress address) throws MemcachedException, InterruptedException { client.flushAllWithNoReply(address); } public void flushAll(InetSocketAddress address, long timeout) throws MemcachedException, InterruptedException, TimeoutException { client.flushAll(address, timeout); } @Deprecated public void flushAll(String host) throws TimeoutException, InterruptedException, MemcachedException { client.flushAll(host); } public Map<String, String> stats(InetSocketAddress address) throws MemcachedException, InterruptedException, TimeoutException { return client.stats(address); } public Map<String, String> stats(InetSocketAddress address, long timeout) throws MemcachedException, InterruptedException, TimeoutException { return client.stats(address, timeout); } public Map<InetSocketAddress, Map<String, String>> getStats(long timeout) throws MemcachedException, InterruptedException, TimeoutException { return client.getStats(timeout); } public Map<InetSocketAddress, Map<String, String>> getStats() throws MemcachedException, InterruptedException, TimeoutException { return client.getStats(); } public Map<InetSocketAddress, Map<String, String>> getStatsByItem( String itemName) throws MemcachedException, InterruptedException, TimeoutException { return client.getStatsByItem(itemName); } public void shutdown() throws IOException { client.shutdown(); } public boolean delete(String key) throws TimeoutException, InterruptedException, MemcachedException { return client.delete(key); } @SuppressWarnings("rawtypes") public Transcoder getTranscoder() { return client.getTranscoder(); } @SuppressWarnings("rawtypes") public void setTranscoder(Transcoder transcoder) { client.setTranscoder(transcoder); } public Map<InetSocketAddress, Map<String, String>> getStatsByItem( String itemName, long timeout) throws MemcachedException, InterruptedException, TimeoutException { return client.getStatsByItem(itemName, timeout); } public long getOpTimeout() { return client.getOpTimeout(); } public void setOpTimeout(long opTimeout) { client.setOpTimeout(opTimeout); } public Map<InetSocketAddress, String> getVersions(long timeout) throws TimeoutException, InterruptedException, MemcachedException { return client.getVersions(timeout); } @Deprecated public Collection<InetSocketAddress> getAvaliableServers() { return client.getAvaliableServers(); } public Collection<InetSocketAddress> getAvailableServers() { return client.getAvailableServers(); } public void addServer(String server, int port, int weight) throws IOException { client.addServer( server, port, weight); } public void addServer(InetSocketAddress inetSocketAddress, int weight) throws IOException { client.addServer(inetSocketAddress, weight); } @Deprecated public void deleteWithNoReply(String key, int time) throws InterruptedException, MemcachedException { client.deleteWithNoReply(key, time); } public void deleteWithNoReply(String key) throws InterruptedException, MemcachedException { client.deleteWithNoReply(key); } public void incrWithNoReply(String key, long delta) throws InterruptedException, MemcachedException { client.incrWithNoReply(key, delta); } public void decrWithNoReply(String key, long delta) throws InterruptedException, MemcachedException { client.decrWithNoReply(key, delta); } public void setLoggingLevelVerbosity(InetSocketAddress address, int level) throws TimeoutException, InterruptedException, MemcachedException { client.setLoggingLevelVerbosity(address, level); } public void setLoggingLevelVerbosityWithNoReply(InetSocketAddress address, int level) throws InterruptedException, MemcachedException { client.setLoggingLevelVerbosityWithNoReply(address, level); } public void addStateListener(MemcachedClientStateListener listener) { client.addStateListener(listener); } public void removeStateListener(MemcachedClientStateListener listener) { client.removeStateListener(listener); } public Collection<MemcachedClientStateListener> getStateListeners() { return client.getStateListeners(); } public void flushAllWithNoReply(int exptime) throws InterruptedException, MemcachedException { client.flushAllWithNoReply(exptime); } public void flushAll(int exptime, long timeout) throws TimeoutException, InterruptedException, MemcachedException { client.flushAll(exptime, timeout); } public void flushAllWithNoReply(InetSocketAddress address, int exptime) throws MemcachedException, InterruptedException { client.flushAllWithNoReply(address, exptime); } public void flushAll(InetSocketAddress address, long timeout, int exptime) throws MemcachedException, InterruptedException, TimeoutException { client.flushAll(address, timeout, exptime); } public void setHealSessionInterval(long healConnectionInterval) { client.setHealSessionInterval(healConnectionInterval); } public void setEnableHealSession(boolean enableHealSession) { client.setEnableHealSession(enableHealSession); } public long getHealSessionInterval() { return client.getHealSessionInterval(); } public Protocol getProtocol() { return client.getProtocol(); } public void setPrimitiveAsString(boolean primitiveAsString) { client.setPrimitiveAsString(primitiveAsString); } public void setConnectionPoolSize(int poolSize) { client.setConnectionPoolSize(poolSize); } public void setEnableHeartBeat(boolean enableHeartBeat) { client.setEnableHeartBeat(enableHeartBeat); } public void setSanitizeKeys(boolean sanitizeKey) { client.setSanitizeKeys(sanitizeKey); } public boolean isSanitizeKeys() { return client.isSanitizeKeys(); } public Counter getCounter(String key) { return client.getCounter(key); } public Counter getCounter(String key, long initialValue) { return client.getCounter(key, initialValue); } @Deprecated public KeyIterator getKeyIterator(InetSocketAddress address) throws MemcachedException, InterruptedException, TimeoutException { return client.getKeyIterator(address); } public void setAuthInfoMap(Map<InetSocketAddress, AuthInfo> map) { client.setAuthInfoMap(map); } public Map<InetSocketAddress, AuthInfo> getAuthInfoMap() { return client.getAuthInfoMap(); } public long decr(String key, long delta, long initValue, long timeout, int exp) throws TimeoutException, InterruptedException, MemcachedException { return client.decr(key, delta, initValue, timeout, exp); } public long incr(String key, long delta, long initValue, long timeout, int exp) throws TimeoutException, InterruptedException, MemcachedException { return client.incr(key, delta, initValue, timeout, exp); } public String getName() { return client.getName(); } public void setName(String name) { client.setName(name); } public Queue<ReconnectRequest> getReconnectRequestQueue() { return client.getReconnectRequestQueue(); } public void setFailureMode(boolean failureMode) { client.setFailureMode(failureMode); } public boolean isFailureMode() { return client.isFailureMode(); } public void setKeyProvider(KeyProvider keyProvider) { client.setKeyProvider(keyProvider); } public int getTimeoutExceptionThreshold() { return client.getTimeoutExceptionThreshold(); } public void setTimeoutExceptionThreshold(int timeoutExceptionThreshold) { client.setTimeoutExceptionThreshold(timeoutExceptionThreshold); } public void invalidateNamespace(String ns) throws MemcachedException, InterruptedException, TimeoutException { client.invalidateNamespace(ns); } public void invalidateNamespace(String ns, long opTimeout) throws MemcachedException, InterruptedException, TimeoutException { client.invalidateNamespace(ns, opTimeout); } public void endWithNamespace() { client.endWithNamespace(); } public void beginWithNamespace(String ns) { client.beginWithNamespace(ns); } public <T> T withNamespace(String ns, MemcachedClientCallable<T> callable) throws MemcachedException, InterruptedException, TimeoutException { return client.withNamespace(ns, callable); } }
package com.save.mc.api.operation; import com.save.mc.api.bugfix.AbsCASOpt; public class SaveCASOperation<T> extends AbsCASOpt<T> { private int exp = 0; private T value; public SaveCASOperation(T value) { super(); this.value = value; } public SaveCASOperation(int exp, T value) { super(); this.exp = exp; this.value = value; } public int initExp() { return this.exp; } @Override public T initValue() { return this.value; } @Override public T getNewValue(T current) { return this.value; } }
package com.save.mc.api.operation; import java.math.BigDecimal; public class SaveAccumulateCASOperation<T> extends SaveCASOperation<T> { public SaveAccumulateCASOperation(T value) { super(value); } public SaveAccumulateCASOperation(int exp, T value) { super(exp, value); } @SuppressWarnings("unchecked") @Override public T getNewValue(T current) { Object t = null; if (current instanceof Integer){ t = (Integer)current + (Integer)this.initValue(); } else if (current instanceof Long){ t = (Long)current + (Long)this.initValue(); } else if (current instanceof Double){ t = (Double)current + (Double)this.initValue(); } else if (current instanceof Float){ t = (Float)current + (Float)this.initValue(); } else if (current instanceof BigDecimal){ t = ((BigDecimal)current).add((BigDecimal)this.initValue()); } return (T) t; } }
package com.save.mc.api.bugfix; /** * 缓存原子性操作接口. * * @author yangz * @date 2013-9-3 上午9:55 */ public abstract class AbsCASOpt<T> { public static final int MAX_TRIES = 8192; /** * 初始值. * * @return */ public abstract T initValue(); /** * 初始值过期时间, 0为永不过期. * * @return */ public abstract int initExp(); /** * 获取新值. * * @param current * @return */ public abstract T getNewValue(T current); }
package com.save.mc.api.bugfix; import net.rubyeye.xmemcached.CASOperation; /** * xmemcache 原子性操作. * @author yangz * @date 2013-9-3 下午4:50 */ public interface XCASOperation<T> extends CASOperation<T>{ /** * 操作成功后的值. * @return */ public T getLastValue(); }
package com.save.mc.api.bugfix; public class UnCaughtException extends Exception { /** * serialVersionUID:TODO(用一句话描述这个变量表示什么) * * @since Ver 1.1 */ private static final long serialVersionUID = 1L; public UnCaughtException(String e) { super(e); } public UnCaughtException(Exception e) { super(e); } }
需要说明的是,提供SaveMemcachedClient.setClient(client)方法的目的是为了方便项目通过配置页面变更Memcached地址而提供的方法:使用如下:
SaveMemcachedClient.INSTANCE.setClient(XMemcachedManager.getInstance());
XMemcachedManager.getInstance()方法需要自己实现,动态读配置。