Java tutorial
/** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.agileEAP.module.cache.memcached; import java.util.Collection; import java.util.Map; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import net.spy.memcached.MemcachedClient; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.DisposableBean; /** * SpyMemcached Client?,??Get/GetBulk/Set/Delete/Incr/Decr???. * * ????getClient()?SpyMemcachedClient?. * * @author calvin */ public class SpyMemcachedClient implements DisposableBean { private static Logger logger = LoggerFactory.getLogger(SpyMemcachedClient.class); private MemcachedClient memcachedClient; private long shutdownTimeout = 2500; private long updateTimeout = 2500; /** * Get, ??, Null. */ public <T> T get(String key) { try { return (T) memcachedClient.get(key); } catch (RuntimeException e) { handleException(e, key); return null; } } /** * GetBulk, ??. */ public <T> Map<String, T> getBulk(Collection<String> keys) { try { return (Map<String, T>) memcachedClient.getBulk(keys); } catch (RuntimeException e) { handleException(e, StringUtils.join(keys, ",")); return null; } } /** * Set, ?. */ public void set(String key, int expiredTime, Object value) { memcachedClient.set(key, expiredTime, value); } /** * Set, ??updateTimeout, ?false??. */ public boolean safeSet(String key, int expiration, Object value) { Future<Boolean> future = memcachedClient.set(key, expiration, value); try { return future.get(updateTimeout, TimeUnit.MILLISECONDS); } catch (Exception e) { future.cancel(false); } return false; } /** * Delete, ?. */ public void delete(String key) { memcachedClient.delete(key); } /** * Delete, ??updateTimeout, ?false??. */ public boolean safeDelete(String key) { Future<Boolean> future = memcachedClient.delete(key); try { return future.get(updateTimeout, TimeUnit.MILLISECONDS); } catch (Exception e) { future.cancel(false); } return false; } /** * Incr. */ public long incr(String key, int by, long defaultValue) { return memcachedClient.incr(key, by, defaultValue); } /** * Decr. */ public long decr(String key, int by, long defaultValue) { return memcachedClient.decr(key, by, defaultValue); } /** * Incr, ??, key?-1. */ public Future<Long> asyncIncr(String key, int by) { return memcachedClient.asyncIncr(key, by); } /** * Decr, ??, key?-1. */ public Future<Long> asyncDecr(String key, int by) { return memcachedClient.asyncDecr(key, by); } private void handleException(Exception e, String key) { logger.warn("spymemcached client receive an exception with key:" + key, e); } @Override public void destroy() throws Exception { if (memcachedClient != null) { memcachedClient.shutdown(shutdownTimeout, TimeUnit.MILLISECONDS); } } public MemcachedClient getMemcachedClient() { return memcachedClient; } public void setMemcachedClient(MemcachedClient memcachedClient) { this.memcachedClient = memcachedClient; } public void setUpdateTimeout(long updateTimeout) { this.updateTimeout = updateTimeout; } public void setShutdownTimeout(long shutdownTimeout) { this.shutdownTimeout = shutdownTimeout; } }