cleanup,SpyMemcachedClient cleanup

This commit is contained in:
Calvin
2012-03-29 00:22:05 +08:00
parent 34442bc833
commit 48e4e9a053
4 changed files with 16 additions and 13 deletions
@@ -4,7 +4,6 @@ import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -36,10 +35,6 @@ public class GuavaCacheDemo extends SpringTxTestCase {
@Autowired
private AccountManager accountManager;
@Before
public void setup() throws Exception {
}
@Test
public void demo() throws Exception {
////设置缓存最大个数为100,缓存过期时间为5秒
@@ -15,6 +15,8 @@
<property name="opTimeout" value="1000" />
</bean>
</property>
<property name="updateTimeout" value="1000"/>
<property name="shutdownTimeout" value="1000"/>
</bean>
<!-- 演示用的JMemcached, 模拟Memcached服务器 -->
@@ -38,6 +38,7 @@
<input id="submit" class="button" type="submit" value="提交"/>&nbsp;
<input id="cancel" class="button" type="button" value="返回" onclick="history.back()"/>
</div>
<div>(保存后将发送JMS消息通知改动,而消息接收者将发送提醒邮件)</div>
</form:form>
</body>
</html>
@@ -18,7 +18,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
/**
* 对SpyMemcached Client的二次封装,提供常用的Get/GetBulk/Set/Delete/Incr/Decr函数的封装.
* 对SpyMemcached Client的二次封装,提供常用的Get/GetBulk/Set/Delete/Incr/Decr函数的同步与异步操作封装.
*
* 未提供封装的函数可直接调用getClient()取出Spy的原版MemcachedClient来使用.
*
@@ -30,7 +30,9 @@ public class SpyMemcachedClient implements DisposableBean {
private MemcachedClient memcachedClient;
private long shutdownTimeout = 1000;
private long shutdownTimeout = 2500;
private long updateTimeout = 2500;
/**
* Get方法, 转换结果类型并屏蔽异常, 仅返回Null.
@@ -64,12 +66,12 @@ public class SpyMemcachedClient implements DisposableBean {
}
/**
* 安全的Set方法,在1秒内返回结果, 否则返回false并取消操作.
* 安全的Set方法,在updateTimeout秒内返回结果, 否则返回false并取消操作.
*/
public boolean safeSet(String key, Object value, int expiration) {
Future<Boolean> future = memcachedClient.set(key, expiration, value);
try {
return future.get(1, TimeUnit.SECONDS);
return future.get(updateTimeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
future.cancel(false);
}
@@ -84,12 +86,12 @@ public class SpyMemcachedClient implements DisposableBean {
}
/**
* 安全的Set方法,在1秒内返回结果, 否则返回false并取消操作.
* 安全的Delete方法,在updateTimeout秒内返回结果, 否则返回false并取消操作.
*/
public boolean safeDelete(String key) {
Future<Boolean> future = memcachedClient.delete(key);
try {
return future.get(1, TimeUnit.SECONDS);
return future.get(updateTimeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
future.cancel(false);
}
@@ -124,9 +126,8 @@ public class SpyMemcachedClient implements DisposableBean {
return memcachedClient.asyncDecr(key, by);
}
private RuntimeException handleException(Exception e, String key) {
private void handleException(Exception e, String key) {
logger.warn("spymemcached client receive an exception with key:" + key, e);
return new RuntimeException(e);
}
@Override
@@ -144,6 +145,10 @@ public class SpyMemcachedClient implements DisposableBean {
this.memcachedClient = memcachedClient;
}
public void setUpdateTimeout(long updateTimeout) {
this.updateTimeout = updateTimeout;
}
public void setShutdownTimeout(long shutdownTimeout) {
this.shutdownTimeout = shutdownTimeout;
}