01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.oscache.web.filter;
06:
07: import com.opensymphony.oscache.base.CacheEntry;
08: import com.opensymphony.oscache.base.EntryRefreshPolicy;
09: import com.opensymphony.oscache.base.NeedsRefreshException;
10:
11: /**
12: * Checks if a cache filter entry has expired.
13: * This is useful when expires header are used in the response.
14: *
15: * @version $Revision: 411 $
16: * @author <a href="mailto:ltorunski [ AT ] t-online.de">Lars Torunski</a>
17: */
18: public class ExpiresRefreshPolicy implements EntryRefreshPolicy {
19:
20: /** the refresh period (in milliseconds) of a certain cache filter*/
21: private long refreshPeriod;
22:
23: /**
24: * Constructor ExpiresRefreshPolicy.
25: *
26: * @param refreshPeriod the refresh period in seconds
27: */
28: public ExpiresRefreshPolicy(int refreshPeriod) {
29: this .refreshPeriod = refreshPeriod * 1000L;
30: }
31:
32: /**
33: * Indicates whether the supplied <code>CacheEntry</code> needs to be refreshed.
34: * This will be called when retrieving an entry from the cache - if this method
35: * returns <code>true</code> then a <code>NeedsRefreshException</code> will be
36: * thrown.
37: *
38: * @param entry The cache entry which is ignored.
39: * @return <code>true</code> if the content needs refreshing, <code>false</code> otherwise.
40: *
41: * @see NeedsRefreshException
42: * @see CacheEntry
43: */
44: public boolean needsRefresh(CacheEntry entry) {
45:
46: long currentTimeMillis = System.currentTimeMillis();
47:
48: if ((refreshPeriod >= 0)
49: && (currentTimeMillis >= (entry.getLastUpdate() + refreshPeriod))) {
50: return true;
51: } else if (entry.getContent() instanceof ResponseContent) {
52: ResponseContent responseContent = (ResponseContent) entry
53: .getContent();
54: return currentTimeMillis >= responseContent.getExpires();
55: } else {
56: return false;
57: }
58:
59: }
60:
61: /**
62: * @return the refreshPeriod in seconds
63: * @since 2.4
64: */
65: public long getRefreshPeriod() {
66: return refreshPeriod / 1000;
67: }
68:
69: /**
70: * @param refreshPeriod the refresh period in seconds
71: * @since 2.4
72: */
73: public void setRefreshPeriod(long refreshPeriod) {
74: this .refreshPeriod = refreshPeriod * 1000L;
75: }
76:
77: }
|