1 /*
2 * Copyright (c) 2002-2003 by OpenSymphony
3 * All rights reserved.
4 */
5 package com.opensymphony.oscache.web.filter;
6
7 import com.opensymphony.oscache.base.CacheEntry;
8 import com.opensymphony.oscache.base.EntryRefreshPolicy;
9 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) && (currentTimeMillis >= (entry.getLastUpdate() + refreshPeriod))) {
49 return true;
50 } else if (entry.getContent() instanceof ResponseContent) {
51 ResponseContent responseContent = (ResponseContent) entry.getContent();
52 return currentTimeMillis >= responseContent.getExpires();
53 } else {
54 return false;
55 }
56
57 }
58
59 /**
60 * @return the refreshPeriod in seconds
61 * @since 2.4
62 */
63 public long getRefreshPeriod() {
64 return refreshPeriod / 1000;
65 }
66
67 /**
68 * @param refreshPeriod the refresh period in seconds
69 * @since 2.4
70 */
71 public void setRefreshPeriod(long refreshPeriod) {
72 this.refreshPeriod = refreshPeriod * 1000L;
73 }
74
75 }