Query Cache will not always be invalidated (#1717)

* provide failing test, that shows missing cache invalidation

* FIX: proper cache invalidation when dependent table will be modified
This commit is contained in:
Roland Praml
2019-05-20 20:25:18 +12:00
committed by Rob Bygrave
parent c6ba4d8155
commit 0cac723eac
5 changed files with 162 additions and 0 deletions
@@ -1,13 +1,18 @@
package org.tests.cache;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.Ebean;
import io.ebean.Query;
import io.ebean.cache.ServerCache;
import org.junit.Test;
import org.tests.model.basic.Address;
import org.tests.model.basic.Contact;
import org.tests.model.basic.Customer;
import org.tests.model.basic.ResetBasicData;
import org.tests.model.basic.cache.ECacheChild;
import org.tests.model.basic.cache.ECacheRoot;
import java.util.List;
@@ -122,4 +127,69 @@ public class TestQueryCacheTableDependency extends BaseTestCase {
assertThat(custCount1).isEqualTo(0);
}
@Test
public void testCache_withInvalidateQueryCache() throws InterruptedException {
// So Address has no query cache enabled, but has @InvalidateQueryCache annotation
Address root = new Address();
root.setCity("new york");
DB.save(root);
Customer child = new Customer();
child.setName("bobby");
child.setBillingAddress(root);
DB.save(child);
DB.getServerCacheManager().getQueryCache(Address.class).clear();
DB.getServerCacheManager().getQueryCache(Customer.class).clear();
// Test preparation finished, start the test
Thread.sleep(10);
Query<Customer> query = DB.find(Customer.class).where().eq("billingAddress.city", "new york").query();
query.setUseQueryCache(true);
assertThat(query.findCount()).isEqualTo(1);
root.setCity("san francisco");
DB.save(root);
assertThat(query.findCount()).isEqualTo(0);
// clean up
DB.delete(child);
// DB.delete(root); deleted by M2O cascade.
}
@Test
public void testCache_withEnabeldQueryCache() throws InterruptedException {
// ECacheRoot and ECacheChild have enabled query cache.
ECacheRoot root = new ECacheRoot();
root.setName("testRoot");
DB.save(root);
ECacheChild child = new ECacheChild();
child.setName("testChild");
child.setRoot(root);
DB.save(child);
DB.getServerCacheManager().getQueryCache(ECacheChild.class).clear();
DB.getServerCacheManager().getQueryCache(ECacheRoot.class).clear();
// Test preparation finished, start the test
Thread.sleep(10);
// we need this thread.sleep here, because on a fast machine, DB.save(child) and computing the
// queryCacheEntry will happen in the same millisecond (or 10 milliseconds, which is the resolution
// of System.currentTimeMillis() on windows 7 & java 8)
Query<ECacheChild> query = DB.find(ECacheChild.class).where().eq("root.name", "testRoot").query();
query.setUseQueryCache(true);
assertThat(query.findCount()).isEqualTo(1);
root = DB.find(ECacheRoot.class).findList().get(0);
root.setName("test2");
DB.save(root);
assertThat(query.findCount()).isEqualTo(0);
// clean up
DB.delete(child);
DB.delete(root);
}
}
@@ -0,0 +1,52 @@
package org.tests.model.basic.cache;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import io.ebean.annotation.Cache;
@Entity
@Cache(enableQueryCache = true)
public class ECacheChild {
@Id
@GeneratedValue
protected UUID id;
@Size(max = 100)
private String name;
@NotNull
@ManyToOne
private ECacheRoot root;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ECacheRoot getRoot() {
return root;
}
public void setRoot(ECacheRoot root) {
this.root = root;
}
}
@@ -0,0 +1,38 @@
package org.tests.model.basic.cache;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Size;
import io.ebean.annotation.Cache;
@Entity
@Cache(enableQueryCache = true)
public class ECacheRoot {
@Id
@GeneratedValue
protected UUID id;
@Size(max = 100)
private String name;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}