1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.util;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.astrogrid.workflow.beans.v1.execution.JobURN;
17
18 import org.apache.commons.collections.map.LRUMap;
19
20 import java.lang.ref.Reference;
21 import java.lang.ref.SoftReference;
22 import java.util.Map;
23 /***
24 * A caching map - combination of a LeastRecentlyUsed map, with soft references.
25 * As the implementation of soft references suggests that the JVM frees the least recently used reference too, then
26 * we should get a nicely behaved cache - will have the fixed size until memory starts to get tight, where the least recently used items get freed.
27 * @author Noel Winstanley nw@jb.man.ac.uk 12-Apr-2005
28 *
29 */
30
31 public class Cache {
32 /***
33 * Commons Logger for this class
34 */
35 private static final Log logger = LogFactory.getLog(Cache.class);
36
37 public Cache(int size) {
38 m = new LRUMap(size);
39 }
40
41 /*** use a combination of a LRU map and a reference map. */
42 protected final Map m;
43 public void stuff(JobURN key, Object value) {
44 Reference ref = new SoftReference(value);
45 m.put(key.getContent(),ref);
46 }
47
48 public Object check(JobURN urn) {
49 Reference ref = (Reference)m.get(urn.getContent());
50 if (logger.isDebugEnabled()) {
51 logger.debug("Cache " + (ref != null && ref.get() != null ? " HIT" : "MISS"));
52 }
53 return ref == null ? null : ref.get();
54 }
55
56 public void delete(JobURN key) {
57 m.remove(key.getContent());
58 }
59
60 public void clear() {
61 m.clear();
62 }
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79