Skip to content

Commit

Permalink
Merge pull request #154 from muyiluop/path-1
Browse files Browse the repository at this point in the history
update: wkcache CacheResult 添加 ignoreNull 的配置项,当值为 true 时,需要缓存的方法的结果为 null 时不缓存
  • Loading branch information
Wizzercn committed Jul 23, 2020
2 parents a495680 + f071c57 commit 7600033
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void filter(InterceptorChain chain) throws Throwable {
if (bytes == null) {
chain.doChain();
obj = chain.getReturn();
// 如果忽略空值,那么不缓存结果
if (null == obj && cacheResult.ignoreNull()) {
chain.setReturnValue(null);
return;
}
if (liveTime > 0) {
redisService().setex((cacheName + ":" + cacheKey).getBytes(), liveTime, Lang.toBytes(obj));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
String cacheKey() default "";

int cacheLiveTime() default 0;

/**
* 是否忽略 Null 值,如果为 true,当结果为 Null 时不进行缓存
*/
boolean ignoreNull() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public void testRemove(TestBean test) {
public void testRemoveAll() {

}

@CacheResult(cacheKey = "null")
public Object testCacheNull(boolean isNull) {
if(isNull) {
return null;
}
return 1;
}

@CacheResult(cacheKey = "ignoreNull",ignoreNull = true)
public Object testCacheIgnoreNull(boolean isNull) {
if(isNull) {
return null;
}
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ public void testIocLoader() throws ClassNotFoundException {
}


@Test
public void testCacheNull() throws ClassNotFoundException {
NutIoc ioc = new NutIoc(new ComboIocLoader("*anno", "org.nutz.plugins.wkcache", "*jedis", "*wkcache"));

ioc.getIocContext().save("app", "conf", new ObjectProxy(new PropertiesProxy()));
assertTrue(ioc.getNames().length > 0);
MyCacheTest myCacheTest = ioc.get(MyCacheTest.class);
myCacheTest.testCacheNull(true);
assertNull(myCacheTest.testCacheNull(false));

myCacheTest.testCacheIgnoreNull(true);
assertNotNull(myCacheTest.testCacheIgnoreNull(false));
}
}

0 comments on commit 7600033

Please sign in to comment.