mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor L2 cache tenant awareness - add TenantAwareCache
The design change is to use TenantAwareCache to deal with wrap/unwrap of tenant aware keys, removing any tenant awareness from the underlying cache implementations (DefaultServerCache, redis, hazelcast etc).
This commit is contained in:
+14
-27
@@ -1,5 +1,6 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCacheType;
|
||||
@@ -12,7 +13,7 @@ import org.tests.model.basic.Customer;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DefaultCacheHolderTest {
|
||||
class DefaultCacheHolderTest {
|
||||
|
||||
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
@@ -25,42 +26,30 @@ public class DefaultCacheHolderTest {
|
||||
.with(cacheFactory, new TableModState());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getCache_normal() {
|
||||
|
||||
void getCache_normal() {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options());
|
||||
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(holder, Customer.class);
|
||||
ServerCache cache = cache(holder, Customer.class);
|
||||
ServerCache cache1 = cache(holder, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(holder, Contact.class);
|
||||
ServerCache cache2 = cache(holder, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
|
||||
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type) {
|
||||
return (DefaultServerCache) holder.getCache(type, ServerCacheType.BEAN);
|
||||
private ServerCache cache(DefaultCacheHolder holder, Class<?> type) {
|
||||
return holder.getCache(type, ServerCacheType.BEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
void getCache_multiTenant() throws Exception {
|
||||
CacheManagerOptions builder = options().with(tenantId::get);
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(builder);
|
||||
|
||||
tenantId.set("ten_1");
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
ServerCache cache = cache(holder, Customer.class);
|
||||
cache.put("1", "value-for-tenant1");
|
||||
cache.put("2", "an other value-for-tenant1");
|
||||
|
||||
@@ -109,10 +98,9 @@ public class DefaultCacheHolderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll() {
|
||||
|
||||
void clearAll() {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options());
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
ServerCache cache = cache(holder, Customer.class);
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
holder.clearAll();
|
||||
@@ -121,12 +109,11 @@ public class DefaultCacheHolderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clearAll_multiTenant() {
|
||||
|
||||
void clearAll_multiTenant() {
|
||||
CacheManagerOptions options = options().with(tenantId::get);
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(options);
|
||||
DefaultServerCache cache = cache(holder, Customer.class);
|
||||
ServerCache cache = cache(holder, Customer.class);
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
|
||||
|
||||
+41
-47
@@ -1,5 +1,6 @@
|
||||
package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCache;
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import io.ebean.config.DatabaseConfig;
|
||||
@@ -11,7 +12,7 @@ import org.tests.model.basic.Customer;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
class DefaultServerCacheManagerTest {
|
||||
|
||||
private final ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
@@ -29,7 +30,7 @@ public class DefaultServerCacheManagerTest {
|
||||
|
||||
private final DefaultServerCacheManager multiTenantManager;
|
||||
|
||||
public DefaultServerCacheManagerTest(){
|
||||
DefaultServerCacheManagerTest(){
|
||||
CacheManagerOptions builder = new CacheManagerOptions(clusterManager, new DatabaseConfig(), true);
|
||||
builder.with(new TdTenPro());
|
||||
this.multiTenantManager = new DefaultServerCacheManager(builder);
|
||||
@@ -37,86 +38,79 @@ public class DefaultServerCacheManagerTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void getCache_normal() {
|
||||
void getCache_normal() {
|
||||
ServerCache cache = cache(manager, Customer.class);
|
||||
DefaultServerCache dsc = cache.unwrap(DefaultServerCache.class);
|
||||
assertThat(dsc.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(dsc.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache = cache(manager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
assertThat(cache.getShortName()).isEqualTo("Customer_B");
|
||||
|
||||
DefaultServerCache cache1 = cache(manager, Customer.class);
|
||||
ServerCache cache1 = cache(manager, Customer.class);
|
||||
assertThat(cache1).isSameAs(cache);
|
||||
|
||||
DefaultServerCache cache2 = cache(manager, Contact.class);
|
||||
ServerCache cache2 = cache(manager, Contact.class);
|
||||
assertThat(cache1).isNotSameAs(cache2);
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(cache2.getShortName()).isEqualTo("Contact_B");
|
||||
DefaultServerCache dsc2 = cache2.unwrap(DefaultServerCache.class);
|
||||
assertThat(dsc2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
assertThat(dsc2.getShortName()).isEqualTo("Contact_B");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class);
|
||||
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
assertThat(natKeyCache.getShortName()).isEqualTo("Customer_N");
|
||||
ServerCache natKeyCache = manager.getNaturalKeyCache(Customer.class);
|
||||
DefaultServerCache dscNatKey = natKeyCache.unwrap(DefaultServerCache.class);
|
||||
assertThat(dscNatKey.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
assertThat(dscNatKey.getShortName()).isEqualTo("Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class);
|
||||
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
assertThat(queryCache.getShortName()).isEqualTo("Customer_Q");
|
||||
ServerCache queryCache = manager.getQueryCache(Customer.class);
|
||||
DefaultServerCache dscQueryCache = queryCache.unwrap(DefaultServerCache.class);
|
||||
assertThat(dscQueryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
assertThat(dscQueryCache.getShortName()).isEqualTo("Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
assertThat(collCache.getShortName()).isEqualTo("Customer.contacts_C");
|
||||
ServerCache collCache = manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
DefaultServerCache dscCollCache = collCache.unwrap(DefaultServerCache.class);
|
||||
assertThat(dscCollCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
assertThat(dscCollCache.getShortName()).isEqualTo("Customer.contacts_C");
|
||||
|
||||
cache.clearCount.reset();
|
||||
collCache.clearCount.reset();
|
||||
queryCache.clearCount.reset();
|
||||
natKeyCache.clearCount.reset();
|
||||
cache.statistics(true);
|
||||
collCache.statistics(true);
|
||||
queryCache.statistics(true);
|
||||
natKeyCache.statistics(true);
|
||||
|
||||
manager.clear(Customer.class);
|
||||
|
||||
assertThat(cache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(natKeyCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(queryCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(collCache.clearCount.get(true)).isEqualTo(1);
|
||||
assertThat(cache.statistics(true).getClearCount()).isEqualTo(1);
|
||||
assertThat(natKeyCache.statistics(true).getClearCount()).isEqualTo(1);
|
||||
assertThat(queryCache.statistics(true).getClearCount()).isEqualTo(1);
|
||||
assertThat(collCache.statistics(true).getClearCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType);
|
||||
private ServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return manager.getBeanCache(beanType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() {
|
||||
|
||||
void getCache_multiTenant() {
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
ServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isNull();
|
||||
|
||||
tenantId.set("ten1");
|
||||
|
||||
assertThat(cache.get("1")).isNotNull();
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("tenant1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_singleTenant() {
|
||||
|
||||
void getCache_singleTenant() {
|
||||
ServerCache cache = cache(manager, Customer.class);
|
||||
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() {
|
||||
|
||||
void isLocalL2Caching() {
|
||||
assertTrue(manager.isLocalL2Caching());
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user