Skip to content

Commit

Permalink
Changes to add computeIfAbsent functionality for ehcache disk cache
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar Upadhyaya <[email protected]>
  • Loading branch information
sgup432 committed Jan 12, 2024
1 parent 03c7872 commit e1bf33e
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 63 deletions.
5 changes: 5 additions & 0 deletions server/src/main/java/org/opensearch/common/cache/ICache.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.opensearch.common.cache;

import org.opensearch.common.cache.stats.CacheStats;

/**
* Represents a cache interface.
* @param <K> Type of key.
Expand All @@ -31,4 +33,7 @@ public interface ICache<K, V> {
long count();

void refresh();
void close();

CacheStats stats();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.opensearch.common.cache.LoadAwareCacheLoader;
import org.opensearch.common.cache.RemovalListener;
import org.opensearch.common.cache.RemovalNotification;
import org.opensearch.common.cache.stats.CacheStats;
import org.opensearch.common.cache.store.builders.StoreAwareCacheBuilder;
import org.opensearch.common.cache.store.enums.CacheStoreType;
import org.opensearch.common.cache.store.listeners.StoreAwareCacheEventListener;
Expand All @@ -30,6 +31,8 @@ public class OpenSearchOnHeapCache<K, V> implements StoreAwareCache<K, V>, Remov

private final StoreAwareCacheEventListener<K, V> eventListener;

private final CacheStats stats = new OpenSearchOnHeapCacheStats();

public OpenSearchOnHeapCache(Builder<K, V> builder) {
CacheBuilder<K, V> cacheBuilder = CacheBuilder.<K, V>builder()
.setMaximumWeight(builder.getMaxWeightInBytes())
Expand Down Expand Up @@ -88,7 +91,7 @@ public Iterable<K> keys() {

@Override
public long count() {
return cache.count();
return stats.count();
}

@Override
Expand All @@ -104,6 +107,11 @@ public CacheStoreType getTierType() {
@Override
public void close() {}

@Override
public CacheStats stats() {
return stats;
}

@Override
public void onRemoval(RemovalNotification<K, V> notification) {
eventListener.onRemoval(
Expand All @@ -116,6 +124,16 @@ public void onRemoval(RemovalNotification<K, V> notification) {
);
}

/**
* Stats for opensearch on heap cache.
*/
class OpenSearchOnHeapCacheStats implements CacheStats {
@Override
public long count() {
return cache.count();
}
}

/**
* Builder object
* @param <K> Type of key
Expand Down
Loading

0 comments on commit e1bf33e

Please sign in to comment.