Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* Cache to store object entity.
*/
public class ObjectEntityCache {
private static final int MAX_CACHE_SIZE = 10000;
private final Map<String, Object> resourceCache;
private final Map<Object, String> uuidReverseMap;
private final ReadWriteLock lock = new ReentrantReadWriteLock();

/**
* Constructor.
*/
public ObjectEntityCache() {
resourceCache = new LinkedHashMap<>();
resourceCache = new LinkedHashMap<String, Object>(16, 0.75f, true);
uuidReverseMap = new IdentityHashMap<>();
}

Expand All @@ -33,8 +37,38 @@ public ObjectEntityCache() {
* @return the object
*/
public Object put(String type, String id, Object entity) {
uuidReverseMap.put(entity, id);
return resourceCache.put(getCacheKey(type, id), entity);
lock.writeLock().lock();
try {
uuidReverseMap.put(entity, id);

if (resourceCache.size() >= MAX_CACHE_SIZE) {
evictLRU();
}

return resourceCache.put(getCacheKey(type, id), entity);
} finally {
lock.writeLock().unlock();
}
}

private void evictLRU() {
if (resourceCache.isEmpty()) {
return;
}

String oldestKey = resourceCache.keySet().iterator().next();
Object evictedEntity = resourceCache.get(oldestKey);
resourceCache.remove(oldestKey);
uuidReverseMap.remove(evictedEntity);
}

private String getCacheKeyFromUUID(String uuidValue) {
for (String key : resourceCache.keySet()) {
if (key.endsWith("_" + uuidValue)) {
return key;
}
}
return null;
}

/**
Expand All @@ -45,7 +79,12 @@ public Object put(String type, String id, Object entity) {
* @return object
*/
public Object get(String type, String id) {
return resourceCache.get(getCacheKey(type, id));
lock.writeLock().lock();
try {
return resourceCache.get(getCacheKey(type, id));
} finally {
lock.writeLock().unlock();
}
}

/**
Expand All @@ -55,7 +94,12 @@ public Object get(String type, String id) {
* @return uUID
*/
public String getUUID(Object obj) {
return uuidReverseMap.get(obj);
lock.readLock().lock();
try {
return uuidReverseMap.get(obj);
} finally {
lock.readLock().unlock();
}
}

/**
Expand Down