-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.java
More file actions
190 lines (155 loc) · 5.59 KB
/
Cache.java
File metadata and controls
190 lines (155 loc) · 5.59 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.*;
import java.text.DecimalFormat;
//Use import linked list
//This will be a generic type file
public class Cache<T> {
private static DecimalFormat df = new DecimalFormat("0.0000");
private int size;
private int secondSize;
private LinkedList<T> firstCache;
private LinkedList<T> secondCache;
private int firstCacheHits = 0;
private int firstCacheReferences = 0;
private int secondCacheHits = 0;
private int secondCacheReferences = 0;
//Constructor for single cache - takes in size
public Cache(int size){
firstCache = new LinkedList<T>();
this.size = size;
}
//Constructor for double cache - takes in size
public Cache(int size, int secondSize){
firstCache = new LinkedList<T>();
secondCache = new LinkedList<T>();
this.size = size;
this.secondSize = secondSize;
}
//addObject
public void addObjectFirst(T object){
if(firstCache.size() >= size){
firstCache.removeLast();
}
firstCache.addFirst(object);
}
public void moveToTop(T object){
removeObjectFirst(object);
addObjectFirst(object);
}
public void addObjectSecond(T object){
if(secondCache.size() >= secondSize){
secondCache.removeLast();
}
secondCache.addFirst(object);
}
public boolean hasObjectFirst(T object){
return firstCache.contains(object);
}
public boolean hasObjectSecond(T object){
return secondCache.contains(object);
}
//getObject - returns null if not found
public T getObjectFirst(T object){
for(T item : firstCache){
if(item.equals(object)){
return object;
}
}
return null;
}
//getObjectSecond - returns null if not found
public T getObjectSecond(T object){
for(T item : secondCache){
if(item.equals(object)){
return object;
}
}
return null;
}
public void removeObjectFirst(T object){
firstCache.remove(object);
}
public void removeObjectSecond(T object){
secondCache.remove(object);
}
//clearCache
public void clearCacheFirst(){
firstCache.clear();
}
public void clearCacheSecond(){
secondCache.clear();
}
public String toStringFirst(){
String outputString = "";
for(T item : firstCache){
outputString += item.toString() + " ";
}
return outputString;
}
public String toStringSecond(){
String outputString = "";
for(T item : secondCache){
outputString += item.toString() + " ";
}
return outputString;
}
//methods for hit ratios getting and setting
public void incrementFirstCacheHits(){
firstCacheHits++;
}
public int getFirstCacheHits(){
return firstCacheHits;
}
public void incrementFirstCacheReferences(){
firstCacheReferences++;
}
public int getFirstCacheReferences(){
return firstCacheReferences;
}
public void incrementSecondCacheHits(){
secondCacheHits++;
}
public int getSecondCacheHits(){
return secondCacheHits;
}
public void incrementSecondCacheReferences(){
secondCacheReferences++;
}
public int getSecondCacheReferences(){
return secondCacheReferences;
}
public void toStringFirst(Cache<T> cache, String args1, Long startTime){
int totalReferences = cache.getFirstCacheReferences() + cache.getSecondCacheReferences();
int totalHits = cache.getFirstCacheHits() + cache.getSecondCacheHits();
double globalHitRatio = (double)totalHits / totalReferences;
System.out.println("First level cache with " + args1 + "entries has been created" );
System.out.println("................................");
System.out.println("Total number of references: " + totalReferences);
System.out.println("Total number of cache hits: " + cache.getFirstCacheHits()); //used to be totalHits
System.out.println("The global hit ratio: " + df.format(globalHitRatio));
Long timeTaken = System.currentTimeMillis() - startTime;
System.out.println("Time elapsed: " + timeTaken + " milliseconds");
}
public void toStringBoth(Cache<T> cache, String args1, String args2, Long startTime){
int totalReferences = cache.getFirstCacheReferences();
int totalHits = cache.getFirstCacheHits() + cache.getSecondCacheHits();
double globalHitRatio = (double)totalHits / totalReferences;
double firstCacheHitRatio = (double)cache.getFirstCacheHits() / cache.getFirstCacheReferences();
double secondCacheHitRatio = (double)cache.getSecondCacheHits() / cache.getSecondCacheReferences();
System.out.println("First level cache with " + args1 + "entries has been created" );
System.out.println("Second level cache with " + args2 + "entries has been created" );
System.out.println("................................");
System.out.println("Total number of references: " + totalReferences);
System.out.println("Total number of cache hits: \n" + totalHits);
System.out.println("The global hit ratio: \n" + df.format(globalHitRatio));
System.out.println("");
System.out.println("Number of 1st-level cache references:" + cache.getFirstCacheReferences());
System.out.println("Number of 1st-level cache hits: " + cache.getFirstCacheHits());
System.out.println("1st-level cache hit ratio: " + df.format(firstCacheHitRatio));
System.out.println("");
System.out.println("Number of 2nd-level cache references:" + cache.getSecondCacheReferences());
System.out.println("Number of 2nd-level cache hits: " + cache.getSecondCacheHits());
System.out.println("2nd-level cache hit ratio: " + df.format(secondCacheHitRatio));
Long timeTaken = System.currentTimeMillis() - startTime;
System.out.println("Time elapsed: " + timeTaken + " milliseconds");
}
}