mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-03 08:24:23 +00:00
#493 - ENH: Multi-tenant support - cache tidy up
This commit is contained in:
1 parent
b7c92aec3d
commit
502e9daaf2
7 files changed
+197
-21
No files matched your search
+93
@@ -0,0 +1,93 @@
|
||||
package com.avaje.ebeaninternal.server.cache;
|
||||
|
||||
import com.avaje.ebean.cache.ServerCache;
|
||||
import com.avaje.ebean.cache.ServerCacheFactory;
|
||||
import com.avaje.ebean.cache.ServerCacheOptions;
|
||||
import com.avaje.ebean.cache.ServerCacheType;
|
||||
import com.avaje.ebean.config.CurrentTenantProvider;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultCacheHolderTest {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
private final ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, null);
|
||||
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache.getName()).isEqualTo("customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(holder, Contact.class, "contact");
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("contact_B");
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type, String name) {
|
||||
Supplier<ServerCache> cache = holder.getCache(type, name, ServerCacheType.BEAN);
|
||||
return (DefaultServerCache) cache.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
|
||||
tenantId = "ten_1";
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache.getName()).isEqualTo("customer_ten_1_B");
|
||||
|
||||
tenantId = "ten_2";
|
||||
DefaultServerCache cache2 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache2).isNotSameAs(cache);
|
||||
assertThat(cache2.getName()).isEqualTo("customer_ten_2_B");
|
||||
|
||||
tenantId = "ten_1";
|
||||
DefaultServerCache cache3 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache3).isSameAs(cache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, null);
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
holder.clearAll();
|
||||
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll_multiTenant() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
|
||||
holder.clearAll();
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+1
-1
@@ -19,7 +19,7 @@ public class DefaultCacheHolder_getCacheOptions_Test {
|
||||
defaultOptions.setMaxSize(10000);
|
||||
defaultOptions.setMaxSecsToLive(120);
|
||||
|
||||
this.cacheHolder = new DefaultCacheHolder(null, defaultOptions, null);
|
||||
this.cacheHolder = new DefaultCacheHolder(null, defaultOptions, defaultOptions, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.avaje.ebeaninternal.server.cache;
|
||||
|
||||
import com.avaje.ebean.cache.ServerCacheFactory;
|
||||
import com.avaje.ebean.cache.ServerCacheOptions;
|
||||
import com.avaje.ebean.config.CurrentTenantProvider;
|
||||
import com.avaje.tests.model.basic.Contact;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
|
||||
private DefaultServerCacheManager manager = new DefaultServerCacheManager(true, null, cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
private DefaultServerCacheManager multiTenantManager = new DefaultServerCacheManager(true, new MyTenantProv(), cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("com.avaje.tests.model.basic.Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(manager, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(manager, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("com.avaje.tests.model.basic.Contact_B");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class).get();
|
||||
assertThat(natKeyCache.getName()).isEqualTo("com.avaje.tests.model.basic.Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class).get();
|
||||
assertThat(queryCache.getName()).isEqualTo("com.avaje.tests.model.basic.Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts").get();
|
||||
assertThat(collCache.getName()).isEqualTo("com.avaje.tests.model.basic.Customer.contacts_C");
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
tenantId = "ten1";
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("com.avaje.tests.model.basic.Customer_ten1_B");
|
||||
|
||||
tenantId = "ten2";
|
||||
DefaultServerCache cache1 = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache1).isNotSameAs(cache);
|
||||
assertThat(cache1.getName()).isEqualTo("com.avaje.tests.model.basic.Customer_ten2_B");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isLocalL2Caching() throws Exception {
|
||||
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user