mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-17 14:55:28 +00:00
#1225 - ENH: Extend L2 server cache (near caching) for clearing "near caches"
This commit is contained in:
1 parent
69c045443c
commit
c7f4b56dd1
23 files changed
+585
-65
No files matched your search
@@ -129,7 +129,7 @@ public abstract class BaseTestCase {
|
||||
protected void loadCountryCache() {
|
||||
|
||||
Ebean.find(Country.class)
|
||||
.setBeanCacheMode(CacheMode.RECACHE)
|
||||
.setBeanCacheMode(CacheMode.PUT)
|
||||
.findList();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.ebean.config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ContainerConfigTest {
|
||||
|
||||
@Test
|
||||
public void loadFromProperties() {
|
||||
|
||||
Properties p = new Properties();
|
||||
p.setProperty("ebean.cluster.active", "true");
|
||||
p.setProperty("ebean.cluster.serviceName", "a");
|
||||
p.setProperty("ebean.cluster.namespace", "b");
|
||||
p.setProperty("ebean.cluster.podName", "c");
|
||||
p.setProperty("ebean.cluster.port", "42");
|
||||
|
||||
|
||||
ContainerConfig containerConfig = new ContainerConfig();
|
||||
containerConfig.loadFromProperties(p);
|
||||
|
||||
assertEquals(true, containerConfig.isActive());
|
||||
assertEquals("a", containerConfig.getServiceName());
|
||||
assertEquals("b", containerConfig.getNamespace());
|
||||
assertEquals("c", containerConfig.getPodName());
|
||||
assertEquals(42, containerConfig.getPort());
|
||||
}
|
||||
}
|
||||
+46
-16
@@ -1,10 +1,12 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebeaninternal.server.cluster.ClusterManager;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -13,15 +15,31 @@ public class DefaultServerCacheManagerTest {
|
||||
|
||||
private ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
class TdTenPro implements CurrentTenantProvider {
|
||||
|
||||
private DefaultServerCacheManager manager = new DefaultServerCacheManager(true, null, cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
@Override
|
||||
public Object currentId() {
|
||||
return tenantId.get();
|
||||
}
|
||||
}
|
||||
|
||||
private ClusterManager clusterManager = new ClusterManager(new ContainerConfig());
|
||||
|
||||
private DefaultServerCacheManager manager = new DefaultServerCacheManager(new CacheManagerOptions(clusterManager, new ServerConfig(), true));
|
||||
|
||||
private DefaultServerCacheManager multiTenantManager;
|
||||
|
||||
public DefaultServerCacheManagerTest(){
|
||||
|
||||
CacheManagerOptions builder = new CacheManagerOptions(clusterManager, new ServerConfig(), true);
|
||||
builder.with(new TdTenPro());
|
||||
|
||||
this.multiTenantManager = new DefaultServerCacheManager(builder);
|
||||
}
|
||||
|
||||
private DefaultServerCacheManager multiTenantManager = new DefaultServerCacheManager(true, tenantId::get, cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
|
||||
public void getCache_normal() {
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
@@ -42,6 +60,18 @@ public class DefaultServerCacheManagerTest {
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
|
||||
cache.clearCount.sumThenReset();
|
||||
collCache.clearCount.sumThenReset();
|
||||
queryCache.clearCount.sumThenReset();
|
||||
natKeyCache.clearCount.sumThenReset();
|
||||
|
||||
manager.clear(Customer.class);
|
||||
|
||||
assertThat(cache.clearCount.sumThenReset()).isEqualTo(1);
|
||||
assertThat(natKeyCache.clearCount.sumThenReset()).isEqualTo(1);
|
||||
assertThat(queryCache.clearCount.sumThenReset()).isEqualTo(1);
|
||||
assertThat(collCache.clearCount.sumThenReset()).isEqualTo(1);
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
@@ -49,7 +79,7 @@ public class DefaultServerCacheManagerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
public void getCache_multiTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
@@ -57,32 +87,32 @@ public class DefaultServerCacheManagerTest {
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
|
||||
assertThat(cache.get("1")).isNull();
|
||||
|
||||
tenantId.set("ten1");
|
||||
|
||||
|
||||
assertThat(cache.get("1")).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_singleTenant() throws Exception {
|
||||
public void getCache_singleTenant() {
|
||||
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
|
||||
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("tenant1");
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isLocalL2Caching() throws Exception {
|
||||
public void isLocalL2Caching() {
|
||||
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
|
||||
Reference in new issue
Block a user