1 package org.apache.turbine.services.cache; 2 3 18 19 import java.io.Serializable ; 20 21 import org.apache.turbine.Turbine; 22 23 31 public class CachedObject 32 implements Serializable 33 { 34 35 36 public static final int DEFAULT = 0; 37 38 39 public static final int FOREVER = -1; 40 41 42 private Object contents = null; 43 44 45 private long defaultage = 46 Turbine.getConfiguration() 47 .getLong("cachedobject.defaultage", 1800000); 48 49 50 protected long created = 0; 51 52 53 private long expires = 0; 54 55 56 private boolean stale = false; 57 58 64 public CachedObject(Object o) 65 { 66 this.contents = o; 67 this.expires = defaultage; 68 this.created = System.currentTimeMillis(); 69 } 70 71 78 public CachedObject(Object o, long expires) 79 { 80 if (expires == DEFAULT) 81 { 82 this.expires = defaultage; 83 } 84 85 this.contents = o; 86 this.expires = expires; 87 this.created = System.currentTimeMillis(); 88 } 89 90 95 public Object getContents() 96 { 97 return contents; 98 } 99 100 105 public long getCreated() 106 { 107 return created; 108 } 109 110 115 public long getExpires() 116 { 117 return expires; 118 } 119 120 125 public void setExpires(long expires) 126 { 127 if (expires == DEFAULT) 128 { 129 this.expires = defaultage; 130 } 131 else 132 { 133 this.expires = expires; 134 } 135 if (expires == FOREVER) 136 { 137 setStale(false); 138 } 139 else 140 { 141 setStale((System.currentTimeMillis() - created) > expires); 142 } 143 } 144 145 150 public synchronized void setStale(boolean stale) 151 { 152 this.stale = stale; 153 } 154 155 160 public synchronized boolean getStale() 161 { 162 return stale; 163 } 164 165 170 public synchronized boolean isStale() 171 { 172 if (expires == FOREVER) 173 { 174 return false; 175 } 176 177 setStale((System.currentTimeMillis() - created) > expires); 178 return getStale(); 179 } 180 } 181 | Popular Tags |