001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.oscache.web;
006:
007: import com.opensymphony.oscache.base.Cache;
008: import com.opensymphony.oscache.base.CacheEntry;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import java.io.Serializable;
014:
015: import javax.servlet.http.HttpSessionBindingEvent;
016: import javax.servlet.http.HttpSessionBindingListener;
017:
018: /**
019: * A simple extension of Cache that implements a session binding listener,
020: * and deletes it's entries when unbound
021: *
022: * @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
023: * @author <a href="mailto:tgochenour@peregrine.com">Todd Gochenour</a>
024: * @author <a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
025: * @version $Revision: 339 $
026: */
027: public final class ServletCache extends Cache implements
028: HttpSessionBindingListener, Serializable {
029: private static transient final Log log = LogFactory
030: .getLog(ServletCache.class);
031:
032: /**
033: * The admin for this cache
034: */
035: private ServletCacheAdministrator admin;
036:
037: /**
038: * The scope of that cache.
039: */
040: private int scope;
041:
042: /**
043: * Create a new ServletCache
044: *
045: * @param admin The ServletCacheAdministrator to administer this ServletCache.
046: * @param scope The scope of all entries in this hashmap
047: */
048: public ServletCache(ServletCacheAdministrator admin, int scope) {
049: super (admin.isMemoryCaching(), admin.isUnlimitedDiskCache(),
050: admin.isOverflowPersistence());
051: setScope(scope);
052: this .admin = admin;
053: }
054:
055: /**
056: * Create a new Cache
057: *
058: * @param admin The CacheAdministrator to administer this Cache.
059: * @param algorithmClass The class that implement an algorithm
060: * @param limit The maximum cache size in number of entries
061: * @param scope The cache scope
062: */
063: public ServletCache(ServletCacheAdministrator admin,
064: String algorithmClass, int limit, int scope) {
065: super (admin.isMemoryCaching(), admin.isUnlimitedDiskCache(),
066: admin.isOverflowPersistence(), admin.isBlocking(),
067: algorithmClass, limit);
068: setScope(scope);
069: this .admin = admin;
070: }
071:
072: /**
073: * Get the cache scope
074: *
075: * @return The cache scope
076: */
077: public int getScope() {
078: return scope;
079: }
080:
081: private void setScope(int scope) {
082: this .scope = scope;
083: }
084:
085: /**
086: * When this Cache is bound to the session, do nothing.
087: *
088: * @param event The SessionBindingEvent.
089: */
090: public void valueBound(HttpSessionBindingEvent event) {
091: }
092:
093: /**
094: * When the users's session ends, all listeners are finalized and the
095: * session cache directory is deleted from disk.
096: *
097: * @param event The event that triggered this unbinding.
098: */
099: public void valueUnbound(HttpSessionBindingEvent event) {
100: if (log.isInfoEnabled()) {
101: // CACHE-229: don't access the session's id, because this can throw an IllegalStateException
102: log.info("[Cache] Unbound from session "
103: + event.getSession() + " using name "
104: + event.getName());
105: }
106:
107: admin.finalizeListeners(this );
108: clear();
109: }
110:
111: /**
112: * Indicates whether or not the cache entry is stale. This overrides the
113: * {@link Cache#isStale(CacheEntry, int, String)} method to take into account any
114: * flushing that may have been applied to the scope that this cache belongs to.
115: *
116: * @param cacheEntry The cache entry to test the freshness of.
117: * @param refreshPeriod The maximum allowable age of the entry, in seconds.
118: * @param cronExpiry A cron expression that defines fixed expiry dates and/or
119: * times for this cache entry.
120: *
121: * @return <code>true</code> if the entry is stale, <code>false</code> otherwise.
122: */
123: protected boolean isStale(CacheEntry cacheEntry, int refreshPeriod,
124: String cronExpiry) {
125: return super.isStale(cacheEntry, refreshPeriod, cronExpiry)
126: || admin.isScopeFlushed(cacheEntry, scope);
127: }
128: }
|