1 5 package com.opensymphony.oscache.base; 6 7 import com.opensymphony.oscache.web.filter.ResponseContent; 8 9 import java.io.Serializable ; 10 11 import java.util.Collection ; 12 import java.util.HashSet ; 13 import java.util.Set ; 14 15 26 public class CacheEntry implements Serializable { 27 32 private static final byte NOT_YET = -1; 33 34 41 public static final int INDEFINITE_EXPIRY = -1; 42 43 46 private EntryRefreshPolicy policy = null; 47 48 53 private Object content = null; 54 55 58 private Set groups = null; 59 60 63 private String key; 64 65 68 private boolean wasFlushed = false; 69 70 73 private long created = NOT_YET; 74 75 78 private long lastUpdate = NOT_YET; 79 80 85 public CacheEntry(String key) { 86 this(key, null); 87 } 88 89 96 public CacheEntry(String key, EntryRefreshPolicy policy) { 97 this(key, policy, null); 98 } 99 100 109 public CacheEntry(String key, EntryRefreshPolicy policy, String [] groups) { 110 this.key = key; 111 112 if (groups != null) { 113 this.groups = new HashSet (groups.length); 114 115 for (int i = 0; i < groups.length; i++) { 116 this.groups.add(groups[i]); 117 } 118 } 119 120 this.policy = policy; 121 this.created = System.currentTimeMillis(); 122 } 123 124 133 public synchronized void setContent(Object value) { 134 content = value; 135 lastUpdate = System.currentTimeMillis(); 136 wasFlushed = false; 137 } 138 139 144 public Object getContent() { 145 return content; 146 } 147 148 153 public long getCreated() { 154 return created; 155 } 156 157 162 public synchronized void setGroups(String [] groups) { 163 if (groups != null) { 164 this.groups = new HashSet (groups.length); 165 166 for (int i = 0; i < groups.length; i++) { 167 this.groups.add(groups[i]); 168 } 169 } else { 170 this.groups = null; 171 } 172 173 lastUpdate = System.currentTimeMillis(); 174 } 175 176 181 public void setGroups(Collection groups) { 182 if (groups != null) { 183 this.groups = new HashSet (groups); 184 } else { 185 this.groups = null; 186 } 187 188 lastUpdate = System.currentTimeMillis(); 189 } 190 191 198 public Set getGroups() { 199 return groups; 200 } 201 202 207 public String getKey() { 208 return key; 209 } 210 211 216 public void setLastUpdate(long update) { 217 lastUpdate = update; 218 } 219 220 225 public long getLastUpdate() { 226 return lastUpdate; 227 } 228 229 235 public boolean isNew() { 236 return lastUpdate == NOT_YET; 237 } 238 239 248 public int getSize() { 249 int size = (key.length() * 2) + 4; 251 252 if (content.getClass() == String .class) { 253 size += ((content.toString().length() * 2) + 4); 254 } else if (content instanceof ResponseContent) { 255 size += ((ResponseContent) content).getSize(); 256 } else { 257 return -1; 258 } 259 260 return size + 17; 262 } 263 264 270 public void flush() { 271 wasFlushed = true; 272 } 273 274 285 public boolean needsRefresh(int refreshPeriod) { 286 boolean needsRefresh; 287 288 if (lastUpdate == NOT_YET) { 290 needsRefresh = true; 291 } 292 else if (wasFlushed) { 294 needsRefresh = true; 295 } else if (refreshPeriod == 0) { 296 needsRefresh = true; 297 } 298 else if (policy != null) { 300 needsRefresh = policy.needsRefresh(this); 301 } 302 else if ((refreshPeriod >= 0) && (System.currentTimeMillis() >= (lastUpdate + (refreshPeriod * 1000L)))) { 304 needsRefresh = true; 305 } else { 306 needsRefresh = false; 307 } 308 309 return needsRefresh; 310 } 311 } 312 | Popular Tags |