-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicCache.java
67 lines (55 loc) · 1.68 KB
/
BasicCache.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
61
62
63
64
65
66
67
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.LinkedList;
/**
* Basic FIFO(-ish) caching class with a default size.
*/
public class BasicCache extends UnicastRemoteObject implements ICache
{
private static final long serialVersionUID = 1L;
LinkedList<String> list; // list to implement FIFO
HashMap<String, String> map; // map to look up values
int size; // size of the cache
public BasicCache(int size) throws RemoteException
{
// size must be sane
if (size < 1) size = 1;
// set the size of the cache
this.size = size;
// Make sure the hashing algorithm is fast enough (0.8 is quite low),
// but make sure we will not reallocate (1.3*0.8 = 1.04)
map = new HashMap<String, String>( (int)(size * 1.30) , 0.8f);
// build a simple linked list
list = new LinkedList<String>();
}
public BasicCache() throws RemoteException
{
// default to size 50
this(50);
}
public void clear() throws RemoteException
{
map.clear();
list.clear();
}
public String get(String key) throws RemoteException
{
System.out.println("Locally getting '" + key + "'...");
// retrieve the value from the map
return map.get(key);
}
public void set(String key, String val) throws RemoteException
{
System.out.println("Locally setting '" + key + "' to '" + val + "'...");
// if the key is not in the map yet, some extra work needs to be done
if (!map.containsKey(key))
{
// if the cache is full, remove the last element from the map (FIFO)
while (list.size() >= size) map.remove(list.removeLast());
list.addFirst(key);
}
// store the value in the map
map.put(key, val);
}
}