MCPcopy Create free account
hub / github.com/TrsstProject/trsst / CachingStorage

Class CachingStorage

src/main/java/com/trsst/server/CachingStorage.java:31–229  ·  view source on GitHub ↗

A simple passthrough that caches read operations. @author mpowers

Source from the content-addressed store, hash-verified

29 * @author mpowers
30 */
31public class CachingStorage implements Storage {
32
33 /**
34 * Persistent storage delegate used to fetch items not in cache and to
35 * passthrough write operations.
36 */
37 private Storage persistentStorage;
38
39 /**
40 * Persistent storage delegate used to fetch items not in cache and to
41 * passthrough write operations.
42 */
43 private ConcurrentMap<String, Object> cache;
44
45 /**
46 * Manages index and calls to the specified storage delegate to handle
47 * individual feed, entry, and resource persistence.
48 *
49 * @param delegate
50 * @throws IOException
51 */
52 public CachingStorage(Storage delegate) throws IOException {
53 persistentStorage = delegate;
54 cache = new ConcurrentLinkedHashMap.Builder<String, Object>()
55 .maximumWeightedCapacity(256).build();
56
57 }
58
59 private static char DELIMITER = 0;
60
61 private static final String tokenize(Object... args) {
62 StringBuffer buf = new StringBuffer();
63 for (Object arg : args) {
64 buf.append(arg).append(DELIMITER);
65 }
66 return buf.toString();
67 }
68
69 private static Object NOT_FOUND = "NOT_FOUND";
70
71 private Object get(String token) {
72 if (cache.containsKey(token)) {
73 return cache.get(token);
74 }
75 return NOT_FOUND;
76 }
77
78 private void put(String token, Object value) {
79 cache.put(token, value);
80 }
81
82 private void purge(String prefix) {
83 // purge all keys with prefix
84 for (String key : cache.keySet()) {
85 if (key.startsWith(prefix)) {
86 cache.remove(key);
87 }
88 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected