mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1211 - ENH: Support L2 cache complex natural keys include findList() queries
This commit is contained in:
@@ -3,7 +3,6 @@ package org.tests.batchload;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheManager;
|
||||
import io.ebean.cache.ServerCacheStatistics;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
@@ -18,6 +17,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class TestBatchLazyWithCacheHits extends BaseTestCase {
|
||||
|
||||
private ServerCache beanCache = server().getServerCacheManager().getBeanCache(UUOne.class);
|
||||
|
||||
private UUOne insert(String name) {
|
||||
UUOne one = new UUOne();
|
||||
one.setName("testBLWCH" + name);
|
||||
@@ -28,24 +29,17 @@ public class TestBatchLazyWithCacheHits extends BaseTestCase {
|
||||
@Test
|
||||
public void testOnCacheHit() {
|
||||
|
||||
ArrayList<UUOne> inserted = new ArrayList<>();
|
||||
String[] names = "A,B,C,D,E,F,G,H,I,J".split(",");
|
||||
for (String name : names) {
|
||||
inserted.add(insert(name));
|
||||
}
|
||||
ArrayList<UUOne> inserted = insertData();
|
||||
|
||||
ServerCacheManager serverCacheManager = Ebean.getDefaultServer().getServerCacheManager();
|
||||
ServerCache beanCache = serverCacheManager.getBeanCache(UUOne.class);
|
||||
beanCache.clear();
|
||||
beanCache.getStatistics(true); // Reset statistics - otherwise other tests will interfere
|
||||
clearCacheAndStatistics();
|
||||
|
||||
UUOne b = Ebean.find(UUOne.class, inserted.get(1).getId());
|
||||
assertNotNull(b);
|
||||
|
||||
UUOne b2 = Ebean.find(UUOne.class, inserted.get(1).getId());
|
||||
assertNotNull(b2);
|
||||
ServerCacheStatistics statistics = beanCache.getStatistics(true);
|
||||
assertEquals(statistics.getHitCount(), 1);
|
||||
|
||||
assertBeanCacheHits(1);
|
||||
|
||||
UUOne c = Ebean.find(UUOne.class)
|
||||
.where().idEq(inserted.get(2).getId())
|
||||
@@ -55,9 +49,9 @@ public class TestBatchLazyWithCacheHits extends BaseTestCase {
|
||||
UUOne c2 = Ebean.find(UUOne.class)
|
||||
.where().idEq(inserted.get(2).getId())
|
||||
.findOne();
|
||||
|
||||
assertNotNull(c2);
|
||||
statistics = beanCache.getStatistics(true);
|
||||
assertEquals(statistics.getHitCount(), 1);
|
||||
assertBeanCacheHits(1);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
@@ -76,13 +70,33 @@ public class TestBatchLazyWithCacheHits extends BaseTestCase {
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
System.out.println("sql:" + sql);
|
||||
|
||||
// batch lazy loading into cache
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("from uuone t0 where t0.name like ");
|
||||
platformAssertIn(sql.get(1), "from uuone t0 where t0.id");
|
||||
|
||||
statistics = beanCache.getStatistics(true);
|
||||
assertThat(statistics.getSize()).isGreaterThan(3);
|
||||
// not lazy loading into bean cache
|
||||
int size = beanCache.getStatistics(true).getSize();
|
||||
assertThat(size).isEqualTo(2);
|
||||
}
|
||||
|
||||
private void assertBeanCacheHits(int hits) {
|
||||
ServerCacheStatistics statistics = beanCache.getStatistics(true);
|
||||
assertEquals(statistics.getHitCount(), hits);
|
||||
}
|
||||
|
||||
private void clearCacheAndStatistics() {
|
||||
|
||||
beanCache.clear();
|
||||
beanCache.getStatistics(true);
|
||||
}
|
||||
|
||||
private ArrayList<UUOne> insertData() {
|
||||
ArrayList<UUOne> inserted = new ArrayList<>();
|
||||
String[] names = "A,B,C,D,E,F,G,H,I,J".split(",");
|
||||
for (String name : names) {
|
||||
inserted.add(insert(name));
|
||||
}
|
||||
return inserted;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user