-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: TenanAwareKey vs not tenant aware keys in cache #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.ebean.hazelcast; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import io.ebean.cache.ServerCache; | ||
import io.ebean.cache.ServerCacheStatistics; | ||
import io.ebean.cache.TenantAwareKey; | ||
|
||
public class TenantAwareCache implements ServerCache { | ||
|
||
private final ServerCache delegate; | ||
private final TenantAwareKey tenantAwareKey; | ||
|
||
public TenantAwareCache(ServerCache delegate, TenantAwareKey tenantAwareKey) { | ||
this.delegate = delegate; | ||
this.tenantAwareKey = tenantAwareKey; | ||
} | ||
|
||
private Object key(Object key) { | ||
return tenantAwareKey.key(key); | ||
} | ||
@Override | ||
public Object get(Object id) { | ||
return delegate.get(key(id)); | ||
} | ||
|
||
@Override | ||
public void put(Object id, Object value) { | ||
delegate.put(key(id), value); | ||
} | ||
|
||
@Override | ||
public void remove(Object id) { | ||
delegate.remove(key(id)); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
delegate.clear(); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return delegate.size(); | ||
} | ||
|
||
@Override | ||
public int getHitRatio() { | ||
return delegate.getHitRatio(); | ||
} | ||
|
||
@Override | ||
public ServerCacheStatistics getStatistics(boolean reset) { | ||
return delegate.getStatistics(reset); | ||
} | ||
|
||
@Override | ||
public Map<Object, Object> getAll(Set<Object> keys) { | ||
Map<Object, Object> keyMapping = new HashMap<>(); | ||
keys.forEach(k -> keyMapping.put(key(k),k)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to convert the keyset (e.g. and we have to do remove the tenant-info after we get the result form the cache. this could be probably done by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that looks better to me. |
||
Map<Object, Object> tmp = delegate.getAll(keyMapping.keySet()); | ||
Map<Object, Object> ret = new HashMap<Object, Object>(); | ||
tmp.forEach((k,v)-> ret.put(keyMapping.get(k), v)); // remove tenant info here | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void putAll(Map<Object, Object> keyValues) { | ||
Map<Object, Object> tmp = new HashMap<Object, Object>(); | ||
keyValues.forEach((k,v)-> tmp.put(key(k), v)); | ||
delegate.putAll(tmp); | ||
} | ||
|
||
@Override | ||
public void removeAll(Set<Object> keys) { | ||
delegate.removeAll(keys.stream().map(this::key).collect(Collectors.toSet())); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rbygrave I would suggest to move that code to Ebean and remove cache handling form the plugin completely. It acts as a proxy and converts all keys to be tenant-aware.
Unfortunately, we have to do some more or less expensive copy operations on getAll/putAll/removeAll methods.
Note: this kind of bug is also present in other cache plugin(s), e.g. here: https:/ebean-orm/ebean-ignite/blob/master/src/main/java/io/ebean/ignite/IgCache.java#L49