001: package com.completex.objective.components.ocache.impl;
002:
003: import com.completex.objective.components.ocache.OdalCache;
004: import com.completex.objective.util.TimeInterval;
005:
006: /**
007: * @author Gennady Krizhevsky
008: */
009: public abstract class AbstractExpiringCache implements OdalCache {
010:
011: public static final String TIME_PATTERN = "\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d";
012:
013: private String cacheFlushInterval;
014: private String entryFlushInterval;
015: private long cacheFlushIntervalMs = Long.MAX_VALUE;
016: private long entryFlushIntervalMs = Long.MAX_VALUE;
017:
018: private long cacheStartTime;
019:
020: protected AbstractExpiringCache() {
021: resetCacheStartTime();
022: }
023:
024: public final synchronized Object get(Object key) {
025: ExpiringValue value = null;
026: if (!isExpired()) {
027: value = doGet(key);
028: if (value != null) {
029: if (value.isExpired()) {
030: remove(key);
031: value = null;
032: }
033: }
034: } else {
035: clear();
036: resetCacheStartTime();
037: }
038: return value == null ? null : value.value;
039: }
040:
041: private void resetCacheStartTime() {
042: cacheStartTime = System.currentTimeMillis();
043: }
044:
045: protected abstract ExpiringValue doGet(Object key);
046:
047: public final synchronized Object put(Object key, Object value) {
048: return doPut(key, new ExpiringValue(entryFlushIntervalMs,
049: System.currentTimeMillis(), value));
050: }
051:
052: protected abstract Object doPut(Object key, Object value);
053:
054: public boolean isExpired() {
055: long lifeTime = System.currentTimeMillis() - cacheStartTime;
056: return lifeTime >= cacheFlushIntervalMs;
057: }
058:
059: public long getCacheFlushIntervalMs() {
060: return cacheFlushIntervalMs;
061: }
062:
063: public void setCacheFlushIntervalMs(long cacheFlushIntervalMs) {
064: this .cacheFlushIntervalMs = cacheFlushIntervalMs;
065: }
066:
067: public long getEntryFlushIntervalMs() {
068: return entryFlushIntervalMs;
069: }
070:
071: public void setEntryFlushIntervalMs(long entryFlushIntervalMs) {
072: this .entryFlushIntervalMs = entryFlushIntervalMs;
073: }
074:
075: public long getCacheStartTime() {
076: return cacheStartTime;
077: }
078:
079: public void setCacheStartTime(long cacheStartTime) {
080: this .cacheStartTime = cacheStartTime;
081: }
082:
083: /**
084: * Returns Cache Flush Interval in format: HH:mm:ss.SSS following standard
085: * Java Date and Time Patterns (see Java API)
086: *
087: * @return Cache Flush Interval in format: HH:mm:ss.SSS following standard
088: * Java Date and Time Patterns (see Java API)
089: */
090: public String getCacheFlushInterval() {
091: return cacheFlushInterval;
092: }
093:
094: /**
095: * Sets Cache Flush Interval in format: HH:mm:ss.SSS following standard
096: * Java Date and Time Patterns (see Java API)
097: *
098: * @param cacheFlushInterval
099: */
100: public void setCacheFlushInterval(String cacheFlushInterval) {
101: this .cacheFlushInterval = cacheFlushInterval;
102: TimeInterval timeInterval = new TimeInterval(
103: cacheFlushInterval, "cacheFlushInterval");
104: this .cacheFlushIntervalMs = timeInterval.getIntervalMs();
105: }
106:
107: /**
108: * Returns Cache Entry Flush Interval in format: HH:mm:ss.SSS following standard
109: * Java Date and Time Patterns (see Java API)
110: *
111: * @return Cache Entry Flush Interval in format: HH:mm:ss.SSS following standard
112: * Java Date and Time Patterns (see Java API)
113: */
114: public String getEntryFlushInterval() {
115: return entryFlushInterval;
116: }
117:
118: /**
119: * Sets Cache Entry Flush Interval in format: HH:mm:ss.SSS following standard
120: * Java Date and Time Patterns (see Java API)
121: *
122: * @param entryFlushInterval cache entry flush interval
123: */
124: public void setEntryFlushInterval(String entryFlushInterval) {
125: this .entryFlushInterval = entryFlushInterval;
126: TimeInterval timeInterval = new TimeInterval(
127: entryFlushInterval, "entryFlushInterval");
128: this .entryFlushIntervalMs = timeInterval.getIntervalMs();
129: }
130:
131: /**
132: *
133: */
134: public static class ExpiringValue {
135: private long entryLifeSpan;
136: private long startTime;
137: private Object value;
138:
139: public ExpiringValue(long entryLifeSpan, long startTime,
140: Object value) {
141: this .entryLifeSpan = entryLifeSpan;
142: this .startTime = startTime;
143: this .value = value;
144: }
145:
146: public long getStartTime() {
147: return startTime;
148: }
149:
150: public Object getValue() {
151: return value;
152: }
153:
154: public boolean isExpired() {
155: long lifeTime = System.currentTimeMillis() - startTime;
156: return lifeTime >= entryLifeSpan;
157: }
158: }
159:
160: }
|