mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-12-01 15:37:14 +00:00
use composed key in multi tenant environment instead a cache per tenant (#981)
This commit is contained in:
1 parent
0c1a9786a3
commit
d3ea01b76e
12 files changed
+202
-143
No files matched your search
@@ -1,22 +1,18 @@
|
||||
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;
|
||||
import io.ebean.config.CurrentTenantProvider;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.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 ThreadLocal<String> tenantId = new ThreadLocal<>();
|
||||
|
||||
private final ServerCacheFactory cacheFactory = new DefaultServerCacheFactory();
|
||||
private final ServerCacheOptions defaultOptions = new ServerCacheOptions();
|
||||
@@ -38,29 +34,69 @@ public class DefaultCacheHolderTest {
|
||||
}
|
||||
|
||||
private DefaultServerCache cache(DefaultCacheHolder holder, Class<?> type, String name) {
|
||||
Supplier<ServerCache> cache = holder.getCache(type, name, ServerCacheType.BEAN);
|
||||
return (DefaultServerCache) cache.get();
|
||||
return (DefaultServerCache) holder.getCache(type, name, ServerCacheType.BEAN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCache_multiTenant() throws Exception {
|
||||
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, tenantId::get);
|
||||
|
||||
tenantId = "ten_1";
|
||||
tenantId.set("ten_1");
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache.getName()).isEqualTo("customer_ten_1_B");
|
||||
assertThat(cache.getName()).isEqualTo("customer_B");
|
||||
|
||||
tenantId = "ten_2";
|
||||
DefaultServerCache cache2 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache2).isNotSameAs(cache);
|
||||
assertThat(cache2.getName()).isEqualTo("customer_ten_2_B");
|
||||
cache.put("1", "value-for-tenant1");
|
||||
cache.put("2", "an other value-for-tenant1");
|
||||
|
||||
assertThat(cache.size()).isEqualTo(2);
|
||||
|
||||
tenantId.set("ten_2");
|
||||
|
||||
tenantId = "ten_1";
|
||||
DefaultServerCache cache3 = cache(holder, Customer.class, "customer");
|
||||
assertThat(cache3).isSameAs(cache);
|
||||
cache.put("1", "value-for-tenant2");
|
||||
cache.put("2", "an other value-for-tenant2");
|
||||
|
||||
assertThat(cache.size()).isEqualTo(4);
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("value-for-tenant2");
|
||||
assertThat(cache.get("2")).isEqualTo("an other value-for-tenant2");
|
||||
|
||||
|
||||
tenantId.set("ten_1");
|
||||
|
||||
assertThat(cache.get("1")).isEqualTo("value-for-tenant1");
|
||||
assertThat(cache.get("2")).isEqualTo("an other value-for-tenant1");
|
||||
|
||||
Exception exInThread[] = new Exception[1];
|
||||
Thread t = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
assertThat(cache.get("1")).isNull();
|
||||
tenantId.set("ten_2");
|
||||
|
||||
cache.put("1", "value-for-tenant2");
|
||||
cache.put("2", "an other value-for-tenant2");
|
||||
|
||||
tenantId.set(null);
|
||||
|
||||
cache.clear();
|
||||
} catch (Exception e) {
|
||||
exInThread[0] = e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// do some async work
|
||||
t.start();
|
||||
t.join();
|
||||
if (exInThread[0] != null) {
|
||||
throw exInThread[0];
|
||||
}
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void clearAll() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, null);
|
||||
@@ -74,7 +110,7 @@ public class DefaultCacheHolderTest {
|
||||
|
||||
@Test
|
||||
public void clearAll_multiTenant() throws Exception {
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, new MyTenantProv());
|
||||
DefaultCacheHolder holder = new DefaultCacheHolder(cacheFactory, defaultOptions, defaultOptions, tenantId::get);
|
||||
DefaultServerCache cache = cache(holder, Customer.class, "customer");
|
||||
cache.put("foo", "foo");
|
||||
assertThat(cache.size()).isEqualTo(1);
|
||||
@@ -82,12 +118,5 @@ public class DefaultCacheHolderTest {
|
||||
holder.clearAll();
|
||||
assertThat(cache.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
private class MyTenantProv implements CurrentTenantProvider {
|
||||
|
||||
@Override
|
||||
public String currentId() {
|
||||
return tenantId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user