基础工具类-1-CacheUtils

package xxxx;


import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.transcoders.Transcoder;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;


/**
 * 支持集群的缓存
 * 默认的
 * 
 */
public class CacheUtils implements InitializingBean {
private KeyHash keyHash = new KeyHashModImpl();
private final static Log log = LogFactory.getLog(CacheUtils.class);
private static final Object lockObj = new Object();
private Map<Integer, MemcachedClient> memcachedClientMap = new HashMap<Integer, MemcachedClient>();
private boolean address;


public <T> Future<Boolean> add(String key, int exp, T o, Transcoder<T> tc) {
return getMemcachedClient(key).add(key, exp, o, tc);
}
/**
* Add an object to the cache (using the default transcoder)
* iff it does not exist already.
*
* <p>
* The <code>exp</code> value is passed along to memcached exactly as
* given, and will be processed per the memcached protocol specification:
* </p>
*
* <p>Note that the return will be false any time a mutation has not
* occurred.</p>
*
* <blockquote>
* <p>
* The actual value sent may either be
* Unix time (number of seconds since January 1, 1970, as a 32-bit
* value), or a number of seconds starting from current time. In the
* latter case, this number of seconds may not exceed 60*60*24*30 (number
* of seconds in 30 days); if the number sent by a client is larger than
* that, the server will consider it to be real Unix time value rather
* than an offset from current time.
* </p>
* </blockquote>
*
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @return a future representing the processing of this operation
* @throws IllegalStateException in the rare circumstance where queue
*         is too full to accept any more requests
*/
public Future<Boolean> add(String key, int exp, Object o) {
return getMemcachedClient(key).add(key, exp, o);
}
/**
* Set an object in the cache regardless of any existing value.
*
* <p>
* The <code>exp</code> value is passed along to memcached exactly as
* given, and will be processed per the memcached protocol specification:
* </p>
*
* <p>Note that the return will be false any time a mutation has not
* occurred.</p>
*
* <blockquote>
* <p>
* The actual value sent may either be
* Unix time (number of seconds since January 1, 1970, as a 32-bit
* value), or a number of seconds starting from current time. In the
* latter case, this number of seconds may not exceed 60*60*24*30 (number
* of seconds in 30 days); if the number sent by a client is larger than
* that, the server will consider it to be real Unix time value rather
* than an offset from current time.
* </p>
* </blockquote>
*
* @param <T>
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @param tc the transcoder to serialize and unserialize the value
* @return a future representing the processing of this operation
* @throws IllegalStateException in the rare circumstance where queue
*         is too full to accept any more requests
*/
public <T> Future<Boolean> set(String key, int exp, T o, Transcoder<T> tc) {
return getMemcachedClient(key).set(key, exp, o, tc);
}
/**
* Set an object in the cache (using the default transcoder)
* regardless of any existing value.
*
* <p>
* The <code>exp</code> value is passed along to memcached exactly as
* given, and will be processed per the memcached protocol specification:
* </p>
*
* <p>Note that the return will be false any time a mutation has not
* occurred.</p>
*
* <blockquote>
* <p>
* The actual value sent may either be
* Unix time (number of seconds since January 1, 1970, as a 32-bit
* value), or a number of seconds starting from current time. In the
* latter case, this number of seconds may not exceed 60*60*24*30 (number
* of seconds in 30 days); if the number sent by a client is larger than
* that, the server will consider it to be real Unix time value rather
* than an offset from current time.
* </p>
* </blockquote>
*
* @param key the key under which this object should be added.
* @param exp the expiration of this object
* @param o the object to store
* @return a future representing the processing of this operation
* @throws IllegalStateException in the rare circumstance where queue
*         is too full to accept any more requests
*/
public Future<Boolean> set(String key, int exp, Object o) {
return getMemcachedClient(key).set(key, exp, o);
}
public <T> Future<Boolean> replace(String key, int exp, T o, Transcoder<T> tc) {
return getMemcachedClient(key).replace(key, exp, o, tc);
}
public Future<Boolean> replace(String key, int exp, Object o) {
return getMemcachedClient(key).replace(key, exp, o);
}
public Future<Boolean> append(long cas, String key, Object val) {
return getMemcachedClient(key).append(cas, key, val);
}
public <T> Future<Boolean> append(long cas, String key, T val, Transcoder<T> tc) {
return getMemcachedClient(key).append(cas, key, val, tc);
}
public <T> T get(String key, Transcoder<T> tc) {
return getMemcachedClient(key).get(key, tc);
}
public Object get(String key) {
return getMemcachedClient(key).get(key);
}
public Future<Boolean> delete(String key) {
return getMemcachedClient(key).delete(key);
}
public long incr(String key, int by) {
return getMemcachedClient(key).incr(key, by);
}
public long decr(String key, int by) {
return getMemcachedClient(key).decr(key, by);
}
public long incr(String key, int by, long def, int exp) {
return getMemcachedClient(key).incr(key, by, def, exp);
}
public long decr(String key, int by, long def, int exp) {
return getMemcachedClient(key).decr(key, by, def, exp);
}
public long incr(String key, int by, long def) {
return getMemcachedClient(key).incr(key, by, def);
}
public long decr(String key, int by, long def) {
return getMemcachedClient(key).decr(key, by, def);
}
private MemcachedClient getMemcachedClient(String key) {
if (address) {
int intKey = hashKey(key);
if (log.isDebugEnabled())
log.debug(key + " hash via  " + keyHash.getName() + " to " + intKey + " !");
return memcachedClientMap.get(intKey);
}
return null;
}
private int hashKey(String key) {
int addressSize = cacheServerAddress.size();
return keyHash.hashKey(key, addressSize);
}


private List<String> cacheServerAddress;


public void setCacheServerAddress(List<String> cacheServerAddress) {
this.cacheServerAddress = cacheServerAddress;
}
private void init() {
for (int i = 0, cacheServerAddresSize = cacheServerAddress.size(); i < cacheServerAddresSize; i++) {
memcachedClientMap.put(i, getClient(i));
}
}
private MemcachedClient getClient(int i) {
String cacheServerAddre = cacheServerAddress.get(i);
if (StringUtils.isNotBlank(cacheServerAddre)) {
try {
return new MemcachedClient(AddrUtil.getAddresses(cacheServerAddre));
}
catch (Exception e) {
log.error("init memcached client error " + cacheServerAddre, e);
}
}
return null;
}
public void afterPropertiesSet() {
address = cacheServerAddress != null && cacheServerAddress.size() > 0;
if (address) {
try {
init();
}
catch (Exception e) {
log.error("init memcached client error!", e);
}
}
else {
log.error("no memcached address!");
}
}
public void setKeyHash(KeyHash keyHash) {
this.keyHash = keyHash;
}
public void shutdown() {
for (Integer key : memcachedClientMap.keySet()) {
try {
MemcachedClient memcachedClient = memcachedClientMap.get(key);
memcachedClient.shutdown();
}
catch (Exception e) {
log.debug("Memcached shutdown error!", e);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值