diff --git a/opensearch/reference/index-modules/store/remote-file-cache.md b/opensearch/reference/index-modules/store/remote-file-cache.md new file mode 100644 index 00000000000..0f80afc4400 --- /dev/null +++ b/opensearch/reference/index-modules/store/remote-file-cache.md @@ -0,0 +1,91 @@ +# Remote File Cache + +The Remote File Cache module in OpenSearch provides functionality for managing cached files in a distributed environment. This document describes the `getRef` method, which has been added to enhance the cache management capabilities. + +## FileCache + +The `FileCache` class is a key component of the Remote Store module, implementing the `RefCountedCache` interface for managing cached files. + +### getRef Method + +The `getRef` method has been added to the `FileCache` class to retrieve the reference count for a given file path: + +```java +@Override +public Integer getRef(Path key) { + return theCache.getRef(key); +} +``` + +This method delegates to the underlying cache implementation to get the reference count for the specified key (file path). + +## RefCountedCache Interface + +The `RefCountedCache` interface has been updated to include the `getRef` method: + +```java +/** + * Get the reference count for key {@code key}. + */ +Integer getRef(K key); +``` + +This method is implemented by various cache implementations, including `LRUCache` and `SegmentedCache`. + +## Cache Implementations + +### LRUCache + +The `LRUCache` class implements the `getRef` method as follows: + +```java +@Override +public Integer getRef(K key) { + Objects.requireNonNull(key); + lock.lock(); + try { + Node node = data.get(key); + if (node != null) { + return node.refCount; + } + return null; + } finally { + lock.unlock(); + } +} +``` + +This implementation ensures thread-safety by using a lock and returns the reference count of the specified key if it exists in the cache. + +### SegmentedCache + +The `SegmentedCache` class implements the `getRef` method as follows: + +```java +@Override +public Integer getRef(K key) { + if (key == null) throw new NullPointerException(); + return segmentFor(key).getRef(key); +} +``` + +This implementation delegates to the appropriate segment of the cache to retrieve the reference count. + +## Usage + +The `getRef` method can be used to check the reference count of cached items. This is particularly useful for debugging and managing cache entries. For example: + +```java +FileCache fileCache = // initialize FileCache +Path filePath = // specify file path +Integer refCount = fileCache.getRef(filePath); +if (refCount != null) { + System.out.println("Reference count for " + filePath + ": " + refCount); +} else { + System.out.println("File not found in cache: " + filePath); +} +``` + +## Conclusion + +The addition of the `getRef` method to the Remote File Cache module enhances the ability to manage and monitor cached files. This feature provides developers with more control and insight into the state of the file cache, which is crucial for optimizing performance and troubleshooting in OpenSearch deployments that utilize remote storage. \ No newline at end of file