01: package org.shiftone.cache.policy.single;
02:
03: import org.shiftone.cache.util.reaper.ReapableCache;
04:
05: /**
06: * Class SingleCache
07: *
08: *
09: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
10: * @version $Revision: 1.4 $
11: */
12: class SingleCache implements ReapableCache {
13:
14: private long timeoutMilliSeconds = 0;
15: private long expireTime = 0;
16: private Object userKey = null;
17: private Object cacheObject = null;
18:
19: /**
20: * Constructor SingleCache
21: *
22: *
23: * @param timeoutMilliSeconds
24: */
25: public SingleCache(long timeoutMilliSeconds) {
26: this .timeoutMilliSeconds = timeoutMilliSeconds;
27: }
28:
29: /**
30: * Method addObject
31: */
32: public synchronized void addObject(Object userKey,
33: Object cacheObject) {
34:
35: this .userKey = userKey;
36: this .cacheObject = cacheObject;
37: expireTime = System.currentTimeMillis() + timeoutMilliSeconds;
38: }
39:
40: /**
41: * Method getObject
42: */
43: public synchronized Object getObject(Object key) {
44:
45: Object value = null;
46:
47: if ((this .userKey != null) && (key != null)
48: && (key.equals(this .userKey))) {
49: value = this .cacheObject;
50: }
51:
52: return value;
53: }
54:
55: /**
56: * Method size
57: */
58: public int size() {
59:
60: return (userKey == null) ? 0 : 1;
61: }
62:
63: /**
64: * Method remove
65: */
66: public synchronized void remove(Object key) {
67:
68: if ((this .userKey != null) && (key != null)
69: && (key.equals(this .userKey))) {
70: userKey = null;
71: cacheObject = null;
72: }
73: }
74:
75: /**
76: * Method clear
77: */
78: public synchronized void clear() {
79: userKey = null;
80: cacheObject = null;
81: }
82:
83: /**
84: * Method removeExpiredElements
85: */
86: public synchronized void removeExpiredElements() {
87:
88: /// LOG.info("removeExpiredElements");
89: if (System.currentTimeMillis() >= expireTime) {
90: remove(this .userKey);
91: }
92: }
93:
94: public final String toString() {
95: return "SingleCache" + size();
96: }
97: }
|