001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tcclient.cache;
006:
007: import com.tc.logging.TCLogger;
008: import com.tc.object.bytecode.ManagerUtil;
009: import com.tc.util.DebugUtil;
010:
011: import java.io.Serializable;
012:
013: /**
014: * Wrapper for a data value in the cache data store. This wrapper knows
015: * additional information about when the item was created, when it was last
016: * used, when it expires, and whether it is still valid.
017: */
018: public class CacheData implements Serializable {
019: private static final TCLogger logger = ManagerUtil
020: .getLogger("com.tc.cache.CacheData");
021:
022: // Config
023: private final CacheConfig config;
024:
025: // State (not modified)
026: private final Object value;
027: private final Timestamp timestamp; // timestamp contains the time when the CacheData will be expired
028: private final long createTime; // saved at creation time
029:
030: // State (modifiable) - guarded by synchronized methods on "this"
031: private transient long lastAccessedTimeInMillis;
032: private boolean invalidated;
033:
034: /**
035: * @param value Data value, should never null (checked by caller)
036: * @param config Cache configuration information
037: */
038: public CacheData(Object value, CacheConfig config) {
039: this .config = config;
040: this .value = value;
041: this .createTime = System.currentTimeMillis();
042: this .timestamp = new Timestamp(this .createTime, config
043: .getMaxIdleTimeoutMillis(), config.getMaxTTLMillis());
044: this .lastAccessedTimeInMillis = 0;
045: }
046:
047: Timestamp getTimestamp() {
048: return timestamp;
049: }
050:
051: synchronized boolean isValid() {
052: if (invalidated) {
053: return false;
054: }
055: return hasNotExpired() && isStillAlive();
056: }
057:
058: private boolean hasNotExpired() {
059: if (DebugUtil.DEBUG) {
060: log("Client " + ManagerUtil.getClientID() + ", value: "
061: + value + ", maxIdleTimeoutMillis: "
062: + config.getMaxIdleTimeoutMillis()
063: + ", lastAccessedTimeInMillis: "
064: + lastAccessedTimeInMillis + ", idleTimeInMillis: "
065: + getIdleMillis());
066: }
067:
068: if (config.getMaxIdleTimeoutMillis() <= 0) {
069: return true;
070: }
071: return getIdleMillis() < config.getMaxIdleTimeoutMillis();
072: }
073:
074: private boolean isStillAlive() {
075: if (DebugUtil.DEBUG) {
076: log("Client "
077: + ManagerUtil.getClientID()
078: + ", value: "
079: + value
080: + ", maxTTLMillis: "
081: + config.getMaxTTLMillis()
082: + ", createTime: "
083: + createTime
084: + ", timeToDieMillis: "
085: + getTimeToDieMillis()
086: + ", System.currentTimeMillis() <= getTimeToDieMillis(): "
087: + (System.currentTimeMillis() <= getTimeToDieMillis()));
088: }
089:
090: if (config.getMaxTTLMillis() <= 0) {
091: return true;
092: }
093: return System.currentTimeMillis() <= getTimeToDieMillis();
094: }
095:
096: private long getTimeToDieMillis() {
097: return this .createTime + config.getMaxTTLMillis();
098: }
099:
100: synchronized long getIdleMillis() {
101: if (lastAccessedTimeInMillis == 0)
102: return 0;
103: return Math.max(System.currentTimeMillis()
104: - lastAccessedTimeInMillis, 0);
105: }
106:
107: synchronized void finish() {
108: lastAccessedTimeInMillis = System.currentTimeMillis();
109: }
110:
111: synchronized void accessed() {
112: lastAccessedTimeInMillis = System.currentTimeMillis();
113: }
114:
115: public synchronized Object getValue() {
116: return value;
117: }
118:
119: synchronized void invalidate() {
120: this .invalidated = true;
121: }
122:
123: synchronized boolean isInvalidated() {
124: return this .invalidated;
125: }
126:
127: private void log(String msg) {
128: logger.info(msg);
129: }
130:
131: public int hashCode() {
132: return this .value.hashCode();
133: }
134:
135: public boolean equals(Object obj) {
136: if (!(obj instanceof CacheData)) {
137: return false;
138: }
139: CacheData cd = (CacheData) obj;
140: return this.value.equals(cd.value);
141: }
142:
143: }
|