#1681 - Support hitting L2 bean cache via ... findList() with idsIn() expression

This commit is contained in:
rob bygrave
2019-04-26 12:01:16 +12:00
parent 242945aa18
commit 65fa1c056b
17 changed files with 304 additions and 79 deletions
+107
View File
@@ -1,13 +1,21 @@
package org.tests.cache;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Ebean;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheStatistics;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tests.model.basic.Country;
import org.tests.model.basic.OCachedBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
@@ -17,6 +25,10 @@ import static org.junit.Assert.assertNotNull;
*/
public class TestBeanCache extends BaseTestCase {
private static final Logger log = LoggerFactory.getLogger(TestBeanCache.class);
private ServerCache beanCache = DB.getDefault().getServerCacheManager().getBeanCache(OCachedBean.class);
@Test
public void findById_when_idTypeConverted() {
@@ -40,7 +52,102 @@ public class TestBeanCache extends BaseTestCase {
sql = LoggedSqlCollector.stop();
assertNotNull(bean2);
assertThat(sql).isEmpty();
}
@Test
public void idsInExpression() {
List<OCachedBean> beans = createBeans();
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
beanCache.clear();
beanCache.getStatistics(true);
LoggedSqlCollector.start();
log.info("All misses (0 of 3) ...");
List<OCachedBean> list = DB.find(OCachedBean.class)
.where().idIn(ids)
.setUseCache(true)
.findList();
assertThat(list).hasSize(3);
assertBeanCacheHitMiss(0, 3);
List<String> sql = LoggedSqlCollector.current();
assertThat(sql).hasSize(1);
if (isH2()) {
assertThat(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (?, ?, ? )");
}
log.info("All hits (3 of 3) ...");
list = DB.find(OCachedBean.class)
.where().idIn(ids)
.setUseCache(true)
.findList();
assertBeanCacheHitMiss(3, 0);
assertThat(list).hasSize(3);
sql = LoggedSqlCollector.current();
assertThat(sql).hasSize(0); // no misses
// remove a bean so that we get a "partial" hit (2 out of 3 in cache)
beanCache.remove(beans.get(0).getId().toString());
log.info("Partial hits (2 of 3) ...");
list = DB.find(OCachedBean.class)
.where().idIn(ids)
.setUseCache(true)
.findList();
assertBeanCacheHitMiss(2, 1);
assertThat(list).hasSize(3);
sql = LoggedSqlCollector.current();
assertThat(sql).hasSize(1);
if (isH2()) {
// fetch the miss from DB
assertThat(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (? )");
}
// remove beans so that we get a "partial" hit (1 out of 3 in cache)
beanCache.remove(beans.get(1).getId().toString());
beanCache.remove(beans.get(2).getId().toString());
log.info("Partial hits (1 of 3) ...");
list = DB.find(OCachedBean.class)
.where().idIn(ids)
.setUseCache(true)
.findList();
assertBeanCacheHitMiss(1, 2);
assertThat(list).hasSize(3);
sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(1);
if (isH2()) {
// fetch the misses from DB
assertThat(sql.get(0)).contains("from o_cached_bean t0 where t0.id in (?, ? )");
}
}
private void assertBeanCacheHitMiss(int hitCount, int missCount) {
ServerCacheStatistics statistics = beanCache.getStatistics(true);
assertThat(statistics.getHitCount()).isEqualTo(hitCount);
assertThat(statistics.getMissCount()).isEqualTo(missCount);
}
private List<OCachedBean> createBeans() {
List<String> names = Arrays.asList("z0", "z1", "z2");
List<OCachedBean> beans = new ArrayList<>();
for (String name : names) {
OCachedBean bean = new OCachedBean();
bean.setName(name);
beans.add(bean);
}
DB.saveAll(beans);
return beans;
}
@Test