mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
use composed key in multi tenant environment instead a cache per tenant (#981)
This commit is contained in:
committed by
Rob Bygrave
parent
0c1a9786a3
commit
d3ea01b76e
+32
-22
@@ -2,7 +2,6 @@ package io.ebeaninternal.server.cache;
|
||||
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
@@ -12,13 +11,13 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DefaultServerCacheManagerTest {
|
||||
|
||||
private String tenantId;
|
||||
private ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
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());
|
||||
private DefaultServerCacheManager multiTenantManager = new DefaultServerCacheManager(true, tenantId::get, cacheFactory, new ServerCacheOptions(), new ServerCacheOptions());
|
||||
|
||||
@Test
|
||||
public void getCache_normal() throws Exception {
|
||||
@@ -35,31 +34,51 @@ public class DefaultServerCacheManagerTest {
|
||||
assertThat(cache2.getName()).isEqualTo("org.tests.model.basic.Contact_B");
|
||||
|
||||
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class).get();
|
||||
DefaultServerCache natKeyCache = (DefaultServerCache) manager.getNaturalKeyCache(Customer.class);
|
||||
assertThat(natKeyCache.getName()).isEqualTo("org.tests.model.basic.Customer_N");
|
||||
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class).get();
|
||||
DefaultServerCache queryCache = (DefaultServerCache) manager.getQueryCache(Customer.class);
|
||||
assertThat(queryCache.getName()).isEqualTo("org.tests.model.basic.Customer_Q");
|
||||
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts").get();
|
||||
DefaultServerCache collCache = (DefaultServerCache) manager.getCollectionIdsCache(Customer.class, "contacts");
|
||||
assertThat(collCache.getName()).isEqualTo("org.tests.model.basic.Customer.contacts_C");
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultServerCacheManager manager, Class<?> beanType) {
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType).get();
|
||||
return (DefaultServerCache) manager.getBeanCache(beanType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
tenantId = "ten1";
|
||||
tenantId.set("ten1");
|
||||
DefaultServerCache cache = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_ten1_B");
|
||||
assertThat(cache.getName()).isEqualTo("org.tests.model.basic.Customer_B");
|
||||
cache.put("1", "tenant1");
|
||||
|
||||
tenantId = "ten2";
|
||||
DefaultServerCache cache1 = cache(multiTenantManager, Customer.class);
|
||||
assertThat(cache1).isNotSameAs(cache);
|
||||
assertThat(cache1.getName()).isEqualTo("org.tests.model.basic.Customer_ten2_B");
|
||||
tenantId.set("ten2");
|
||||
|
||||
assertThat(cache.get("1")).isNull();
|
||||
|
||||
tenantId.set("ten1");
|
||||
|
||||
assertThat(cache.get("1")).isNotNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_singleTenant() throws Exception {
|
||||
|
||||
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
|
||||
@@ -69,13 +88,4 @@ public class DefaultServerCacheManagerTest {
|
||||
assertTrue(multiTenantManager.isLocalL2Caching());
|
||||
}
|
||||
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user