-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathCache.java
More file actions
64 lines (56 loc) · 2.08 KB
/
Cache.java
File metadata and controls
64 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.lld.inmemorycache.service.impl;
import com.lld.inmemorycache.service.AbstractCache;
import com.lld.inmemorycache.service.EvictionPolicy;
import com.lld.inmemorycache.service.Storage;
public class Cache extends AbstractCache {
public Cache(Storage storage, EvictionPolicy evictionPolicy, int capacity) {
this.storage = storage;
this.evictionPolicy = evictionPolicy;
this.capacity = capacity;
}
@Override
public boolean put(String key, String value) {
if (key == null || value == null)
return false;
if (storage.size() >= capacity) {
// we need to evict keys because the capacity is full.
String keyToEvict = evictionPolicy.getKeyToEvict();
boolean status = storage.remove(keyToEvict);
if (status) {
// eviction complete.
evictionPolicy.keyEvicted(keyToEvict);
} else {
// eviction failed.
// Multiple options here:
// 1. throw exception: not a good option perf wise to throw exceptions.
// 2. ignore the add and expect user to retry
// 3. execute random eviction policy.
// Implementing Option 2.
return false;
}
}
// space present
storage.put(key, value);
evictionPolicy.keyAccessed(key);
return true;
}
// Returns null if Key is not found.
@Override
public String get(String key) {
String value = storage.get(key);
// no point updating statistics for a key that is not present.
// in future maybe we can get some information out of it but for now, skipping it.
if (value != null) {
evictionPolicy.keyAccessed(key);
}
return value;
}
@Override
public boolean remove(String key) {
boolean status = storage.remove(key);
if (status) {
evictionPolicy.keyEvicted(key);
}
return status;
}
}