Skip to content

Commit 6d86851

Browse files
committed
Add getRecordsInfo() API for LRU order inspection
1 parent 2a1edc2 commit 6d86851

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,34 @@ cache.getFreeSpace(); // Free size in cache.
102102
cache.getJournalSize(); // Internal cache journal size in bytes.
103103
```
104104

105+
### Get records information
106+
To get detailed information about cached records including LRU order, use `getRecordsInfo()`.
107+
108+
This returns a list of `RecordInfo` objects sorted by last access time (most recently accessed first).
109+
Records at the end of the list will be evicted first when cache overflows.
110+
111+
```java
112+
List<RecordInfo> records = cache.getRecordsInfo();
113+
for (RecordInfo info : records) {
114+
String key = info.getKey(); // Key used to store the file
115+
String fileName = info.getFileName(); // Actual file name in cache
116+
long size = info.getSize(); // File size in bytes
117+
long lastAccessed = info.getLastAccessed(); // Timestamp of last access
118+
}
119+
```
120+
121+
To get information about a specific record without updating its access time:
122+
123+
```java
124+
RecordInfo info = cache.getRecordInfo("some-key");
125+
if (info != null) {
126+
// Record exists
127+
}
128+
```
129+
130+
**Note:** `getRecordInfo()` and `getRecordsInfo()` do not update the access time,
131+
so they can be used for monitoring without affecting LRU order.
132+
105133
### Thread safety
106134
DiskLruCache is thread-safe. All public methods are synchronized and can be safely called from multiple threads.
107135

cache/src/main/java/com/tomclaw/cache/DiskLruCache.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import java.nio.charset.StandardCharsets;
77
import java.security.MessageDigest;
88
import java.security.NoSuchAlgorithmException;
9+
import java.util.ArrayList;
910
import java.util.HashSet;
11+
import java.util.List;
1012
import java.util.Set;
1113

1214
@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
@@ -154,6 +156,50 @@ public long getJournalSize() {
154156
}
155157
}
156158

159+
/**
160+
* Returns information about all cached records sorted by last access time
161+
* (most recently accessed first). This method does not update access times.
162+
*
163+
* @return list of RecordInfo objects sorted by LRU order
164+
*/
165+
public List<RecordInfo> getRecordsInfo() {
166+
synchronized (journal) {
167+
List<Record> records = journal.getRecordsSortedByTime();
168+
List<RecordInfo> result = new ArrayList<>(records.size());
169+
for (Record record : records) {
170+
result.add(new RecordInfo(
171+
record.getKey(),
172+
record.getName(),
173+
record.getSize(),
174+
record.getTime()
175+
));
176+
}
177+
return result;
178+
}
179+
}
180+
181+
/**
182+
* Returns information about a specific cached record without updating access time.
183+
*
184+
* @param key the key to look up
185+
* @return RecordInfo or null if not found
186+
*/
187+
public RecordInfo getRecordInfo(String key) {
188+
synchronized (journal) {
189+
assertKeyValid(key);
190+
Record record = journal.peek(key);
191+
if (record != null) {
192+
return new RecordInfo(
193+
record.getKey(),
194+
record.getName(),
195+
record.getSize(),
196+
record.getTime()
197+
);
198+
}
199+
return null;
200+
}
201+
}
202+
157203
private static void assertKeyValid(String key) {
158204
if (key == null || key.isEmpty()) {
159205
throw new IllegalArgumentException(String.format("Invalid key value: '%s'", key));

cache/src/main/java/com/tomclaw/cache/Journal.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ public Record get(String key) {
5555
return record;
5656
}
5757

58+
/**
59+
* Returns record without updating access time.
60+
* Used for querying record info without affecting LRU order.
61+
*/
62+
public Record peek(String key) {
63+
return map.get(key);
64+
}
65+
66+
/**
67+
* Returns all records sorted by access time (most recent first).
68+
*/
69+
public List<Record> getRecordsSortedByTime() {
70+
List<Record> records = new ArrayList<>(map.values());
71+
Collections.sort(records, new RecordComparator());
72+
return records;
73+
}
74+
5875
public Record delete(String key) {
5976
Record record = map.remove(key);
6077
if (record != null) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.tomclaw.cache;
2+
3+
/**
4+
* Public information about a cached record.
5+
* This is a snapshot of the record state at the time of retrieval.
6+
*/
7+
@SuppressWarnings("unused")
8+
public class RecordInfo {
9+
10+
private final String key;
11+
private final String fileName;
12+
private final long size;
13+
private final long lastAccessed;
14+
15+
RecordInfo(String key, String fileName, long size, long lastAccessed) {
16+
this.key = key;
17+
this.fileName = fileName;
18+
this.size = size;
19+
this.lastAccessed = lastAccessed;
20+
}
21+
22+
/**
23+
* Returns the key used to store this record.
24+
*/
25+
public String getKey() {
26+
return key;
27+
}
28+
29+
/**
30+
* Returns the actual file name in cache directory.
31+
*/
32+
public String getFileName() {
33+
return fileName;
34+
}
35+
36+
/**
37+
* Returns the file size in bytes.
38+
*/
39+
public long getSize() {
40+
return size;
41+
}
42+
43+
/**
44+
* Returns the timestamp of last access (put or get operation).
45+
* Files with older timestamps will be evicted first (LRU).
46+
*/
47+
public long getLastAccessed() {
48+
return lastAccessed;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "RecordInfo{" +
54+
"key='" + key + '\'' +
55+
", fileName='" + fileName + '\'' +
56+
", size=" + size +
57+
", lastAccessed=" + lastAccessed +
58+
'}';
59+
}
60+
}

0 commit comments

Comments
 (0)