Skip to content

Commit

Permalink
Bug: TenantAwareKey vs not tenant aware keys in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Apr 7, 2022
1 parent c9dcb82 commit a0fbfb2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 48 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.avaje</groupId>
<artifactId>java11-oss</artifactId>
<version>3.6</version>
<version>3.7</version>
</parent>

<groupId>io.ebean</groupId>
<artifactId>ebean-hazelcast</artifactId>
<version>13.0.1-SNAPSHOT</version>
<version>13.2.0-SNAPSHOT</version>

<scm>
<developerConnection>scm:git:[email protected]:ebean-orm/ebean-hazelcast.git</developerConnection>
Expand All @@ -33,7 +33,7 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean</artifactId>
<version>13.0.0</version>
<version>13.2.0</version>
<scope>provided</scope>
</dependency>

Expand All @@ -47,7 +47,7 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test</artifactId>
<version>13.0.0</version>
<version>13.2.0</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -84,7 +84,7 @@
<extensions>true</extensions>
<configuration>
<tiles>
<tile>io.ebean.tile:enhancement:13.0.0</tile>
<tile>io.ebean.tile:enhancement:13.2.0</tile>
</tiles>
</configuration>
</plugin>
Expand Down
32 changes: 5 additions & 27 deletions src/main/java/io/ebean/hazelcast/HzCache.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package io.ebean.hazelcast;

import com.hazelcast.map.IMap;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheStatistics;
import io.ebean.cache.TenantAwareKey;
import io.ebean.config.CurrentTenantProvider;

import java.util.Map;
import java.util.Set;

import com.hazelcast.map.IMap;

/**
* IMap cache implementation for Ebean ServerCache interface.
*/
final class HzCache implements ServerCache {

private final TenantAwareKey tenantAwareKey;
private final IMap<Object, Object> map;

HzCache(IMap<Object, Object> map, CurrentTenantProvider tenantProvider) {
HzCache(IMap<Object, Object> map) {
this.map = map;
this.tenantAwareKey = new TenantAwareKey(tenantProvider);
}

private Object key(Object key) {
return tenantAwareKey.key(key);
}

@Override
public Map<Object, Object> getAll(Set<Object> keys) {
return map.getAll(keys);
Expand All @@ -39,17 +28,17 @@ public void putAll(Map<Object, Object> keyValues) {

@Override
public Object get(Object id) {
return map.get(key(id));
return map.get(id);
}

@Override
public void put(Object id, Object value) {
map.put(key(id), value);
map.put(id, value);
}

@Override
public void remove(Object id) {
map.delete(key(id));
map.delete(id);
}

@Override
Expand All @@ -62,15 +51,4 @@ public int size() {
return map.size();
}

@Override
public int getHitRatio() {
return 0;
}

@Override
public ServerCacheStatistics getStatistics(boolean reset) {
//LocalMapStats localMapStats = map.getLocalMapStats();
return null;
}

}
5 changes: 2 additions & 3 deletions src/main/java/io/ebean/hazelcast/HzCacheFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ public ServerCache createCache(ServerCacheConfig config) {
}

private ServerCache createNormalCache(ServerCacheConfig config) {

String fullName = config.getType().name() + "-" + config.getCacheKey();
logger.debug("get cache [{}]", fullName);
IMap<Object, Object> map = instance.getMap(fullName);
return new HzCache(map, config.getTenantProvider());
return config.tenantAware(new HzCache(map));
}

private ServerCache createQueryCache(ServerCacheConfig config) {
Expand All @@ -130,7 +129,7 @@ private ServerCache createQueryCache(ServerCacheConfig config) {
cache.periodicTrim(executor);
queryCaches.put(config.getCacheKey(), cache);
}
return cache;
return config.tenantAware(cache);
}
}

Expand Down
28 changes: 15 additions & 13 deletions src/test/java/io/ebean/hazelcast/HzCacheFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.ebean.hazelcast;

import io.ebean.DB;
import io.ebean.Database;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheManager;
import io.ebean.cache.TenantAwareCache;
import io.ebean.cache.TenantAwareKey;
import org.example.domain.EAddress;
import org.example.domain.ECustomer;
import org.example.domain.EOrder;
Expand All @@ -17,10 +21,10 @@

public class HzCacheFactoryTest {

private final EbeanServer server;
private final Database server;

public HzCacheFactoryTest() {
server = Ebean.getDefaultServer();
server = DB.getDefault();
}

// @Test
Expand All @@ -39,7 +43,7 @@ public HzCacheFactoryTest() {
@Test
public void setup() {

int count = Ebean.find(ECustomer.class).findCount();
int count = DB.find(ECustomer.class).findCount();
if (count == 0) {

ECustomer customer = new ECustomer("Rob");
Expand All @@ -58,7 +62,7 @@ public void setup() {
@Test
public void run() throws InterruptedException {

ServerCacheManager cacheManager = server.getServerCacheManager();
ServerCacheManager cacheManager = server.cacheManager();
cacheManager.clearAll();

// ECustomer one = new ECustomer("hello");
Expand All @@ -67,7 +71,7 @@ public void run() throws InterruptedException {

for (int i = 0; i < 3; i++) {

List<ECustomer> cust = Ebean.find(ECustomer.class)
List<ECustomer> cust = DB.find(ECustomer.class)
.where().eq("address.city", "Auckland")
.setUseQueryCache(true)
.setReadOnly(true)
Expand All @@ -86,10 +90,11 @@ public void run() throws InterruptedException {
public void integration() throws InterruptedException {


ServerCacheManager cacheManager = server.getServerCacheManager();
ServerCache beanCache = cacheManager.getBeanCache(ECustomer.class);
ServerCacheManager cacheManager = server.cacheManager();
ServerCache beanCache = cacheManager.beanCache(ECustomer.class);

assertThat(beanCache).isInstanceOf(HzCache.class);
assertThat(beanCache).isInstanceOf(TenantAwareCache.class);
assertThat(beanCache.unwrap(HzCache.class)).isInstanceOf(HzCache.class);

// Ebean.update(ECustomer.class)
// .setRaw("name = 'x'")
Expand All @@ -101,13 +106,10 @@ public void integration() throws InterruptedException {
// .where().idEq(1)
// .update();

Ebean.createSqlUpdate("update eaddress set line1 = line1 || 's'")
.execute();

DB.sqlUpdate("update eaddress set line1 = line1 || 's'").execute();
for (int i = 0; i < 5; i++) {

Ebean.createSqlUpdate("update eaddress set line1 = line1 || 's'")
.execute();
DB.sqlUpdate("update eaddress set line1 = line1 || 's'").execute();

// Ebean.update(EAddress.class)
// .setRaw("line1 = line1 || 's'")
Expand Down

0 comments on commit a0fbfb2

Please sign in to comment.