-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadThrough.java
60 lines (51 loc) · 1.25 KB
/
ReadThrough.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
* Read-Through cache reading scheme implementation
*/
public class ReadThrough implements Reader {
/*
* all values that have been passed through or are currently contained in the
* cache
*/
HashSet<Integer> database;
// Cache
Cache cache;
// Recorded parameters
double hits;
double misses;
int database_reads;
int cache_reads;
int cache_writes;
// Initialize
public ReadThrough(Cache cache) {
this.cache = cache;
cache_writes = database_reads = 0;
hits = misses = 0.00;
}
// Getter for CacheWrites
public int getCacheWrites() {
return cache_writes;
}
// Basic mechanism of the ReadThrough cache
public void query(int value) {
if (cache.get(value) == value) {
// Contained, Hit
hits++;
} else {
// Miss, add the value to the cache
misses++;
// Read and write to database and cache
database_reads++;
cache_writes++;
cache.add(value);
}
cache_reads++;
}
// Display all recorded oarams
public void display() {
System.out.println("Hit Rate: " + (hits / (hits + misses)));
System.out.println("Cache Reads: " + cache_reads);
System.out.println("Database Reads: " + database_reads);
}
}