-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheAside.java
60 lines (49 loc) · 1.34 KB
/
CacheAside.java
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
import java.util.HashSet;
/*
* Implementation by Eric Xing
* Cache-Aside implementation of a Reader object
*/
public class CacheAside implements Reader {
// All values contained or previously contained in the cache
HashSet<Integer> database;
// Cache object
Cache cache;
// Tracked values
double hits;
double misses;
int database_reads;
int cache_reads;
int cache_writes;
// Initialize the cache and relevent attributes
public CacheAside(Cache cache) {
this.cache = cache;
cache_writes = database_reads = 0;
hits = misses = 0.00;
}
/*
* Getter function for CacheWrites, as cache writes can come from both Reader
* and Writer
*/
public int getCacheWrites() {
return cache_writes;
}
public void query(int value) {
if (cache.get(value) == value) { // query
hits++; // cache hit
} else {
misses++; // cache miss
// The following are the basic mechanism of the Cache Aside cache
database_reads++;
cache_writes++;
cache.add(value);
}
// The cache gets read no matter
cache_reads++;
}
public void display() {
// Display the basic information about the Cache Reader
System.out.println("Hit Rate: " + (hits / (hits + misses)));
System.out.println("Cache Reads: " + cache_reads);
System.out.println("Database Reads: " + database_reads);
}
}