19
19
*/
20
20
21
21
public class ARCCache <K , V > {
22
- private final int capacity ;
23
22
private final Map <K , V > cache ;
24
23
private final LinkedHashMap <K , Integer > usageCounts ;
25
24
private final int t1Capacity ; // Capacity for the t1 cache
26
25
private final int b1Capacity ; // Capacity for the b1 cache
27
26
private int totalCount ;
28
- /**
29
- * Retrieves the value associated with the given key from the cache.
30
- * If the key is present in the cache, its usage count is incremented.
31
- *
32
- * @param key the key whose associated value is to be retrieved
33
- * @return the value associated with the key, or null if the key is not present in the cache
34
- */
27
+
28
+ /**
29
+ * This constructor initializes an ARCCache object with the given capacity and initializes other necessary fields
30
+ */
35
31
public ARCCache (int capacity ) {
36
- this .capacity = capacity ;
37
32
this .cache = new LinkedHashMap <>();
38
33
this .usageCounts = new LinkedHashMap <>();
39
34
this .t1Capacity = capacity / 2 ; // Capacity for the t1 cache
40
35
this .b1Capacity = capacity - t1Capacity ; // Capacity for the b1 cache
41
36
this .totalCount = 0 ;
42
37
}
43
38
39
+ /**
40
+ * Returns the total capacity of the cache
41
+ *
42
+ * @return the total capacity of the cache
43
+ */
44
+ private int capacity () {
45
+ return t1Capacity + b1Capacity ;
46
+ }
47
+
44
48
/**
45
49
* Retrieves the value associated with the given key from the cache.
46
50
* If the key is present in the cache, its usage count is incremented.
@@ -65,7 +69,7 @@ public V get(K key) {
65
69
* @param value the value to be associated with the specified key
66
70
*/
67
71
public void put (K key , V value ) {
68
- if (cache .size () >= capacity ) {
72
+ if (cache .size () >= capacity () ) {
69
73
evict ();
70
74
}
71
75
cache .put (key , value );
@@ -99,8 +103,8 @@ private void evict() {
99
103
* Adjust the cache sizes based on t1capacity and b1capacity after eviction from cache
100
104
*/
101
105
private void adjustCacheSize () {
102
- if (cache .size () > capacity ) {
103
- int excess = cache .size () - capacity ;
106
+ if (cache .size () > capacity () ) {
107
+ int excess = cache .size () - capacity () ;
104
108
int t1Size = cache .size () - b1Capacity ;
105
109
while (excess > 0 && !cache .isEmpty ()) {
106
110
K keyToRemove = usageCounts .keySet ().iterator ().next ();
0 commit comments