|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ServletCache.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (c) 2002-2003 by OpenSymphony
|
|
| 3 |
* All rights reserved.
|
|
| 4 |
*/
|
|
| 5 |
package com.opensymphony.oscache.web;
|
|
| 6 |
|
|
| 7 |
import com.opensymphony.oscache.base.Cache;
|
|
| 8 |
import com.opensymphony.oscache.base.CacheEntry;
|
|
| 9 |
|
|
| 10 |
import org.apache.commons.logging.Log;
|
|
| 11 |
import org.apache.commons.logging.LogFactory;
|
|
| 12 |
|
|
| 13 |
import java.io.Serializable;
|
|
| 14 |
|
|
| 15 |
import javax.servlet.http.HttpSessionBindingEvent;
|
|
| 16 |
import javax.servlet.http.HttpSessionBindingListener;
|
|
| 17 |
import javax.servlet.jsp.PageContext;
|
|
| 18 |
|
|
| 19 |
/**
|
|
| 20 |
* A simple extension of Cache that implements a session binding listener,
|
|
| 21 |
* and deletes it's entries when unbound
|
|
| 22 |
*
|
|
| 23 |
* @author <a href="mailto:mike@atlassian.com">Mike Cannon-Brookes</a>
|
|
| 24 |
* @author <a href="mailto:tgochenour@peregrine.com">Todd Gochenour</a>
|
|
| 25 |
* @author <a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
|
|
| 26 |
* @version $Revision: 1.3 $
|
|
| 27 |
*/
|
|
| 28 |
public final class ServletCache extends Cache implements HttpSessionBindingListener, Serializable { |
|
| 29 |
private static transient final Log log = LogFactory.getLog(ServletCache.class); |
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* The admin for this cache
|
|
| 33 |
*/
|
|
| 34 |
private ServletCacheAdministrator admin;
|
|
| 35 |
|
|
| 36 |
/**
|
|
| 37 |
* The scope of that cache.
|
|
| 38 |
*/
|
|
| 39 |
private int scope; |
|
| 40 |
|
|
| 41 |
/**
|
|
| 42 |
* Create a new ServletCache
|
|
| 43 |
*
|
|
| 44 |
* @param admin The ServletCacheAdministrator to administer this ServletCache.
|
|
| 45 |
* @param scope The scope of all entries in this hashmap
|
|
| 46 |
*/
|
|
| 47 | 0 |
public ServletCache(ServletCacheAdministrator admin, int scope) { |
| 48 | 0 |
super(admin.isMemoryCaching(), admin.isUnlimitedDiskCache());
|
| 49 | 0 |
setScope(scope); |
| 50 | 0 |
this.admin = admin;
|
| 51 |
} |
|
| 52 |
|
|
| 53 |
/**
|
|
| 54 |
* Create a new Cache
|
|
| 55 |
*
|
|
| 56 |
* @param admin The CacheAdministrator to administer this Cache.
|
|
| 57 |
* @param algorithmClass The class that implement an algorithm
|
|
| 58 |
* @param limit The maximum cache size in number of entries
|
|
| 59 |
* @param scope The cache scope
|
|
| 60 |
*/
|
|
| 61 | 0 |
public ServletCache(ServletCacheAdministrator admin, String algorithmClass, int limit, int scope) { |
| 62 | 0 |
super(admin.isMemoryCaching(), admin.isUnlimitedDiskCache(), admin.isBlocking(), algorithmClass, limit);
|
| 63 | 0 |
setScope(scope); |
| 64 | 0 |
this.admin = admin;
|
| 65 |
} |
|
| 66 |
|
|
| 67 |
/**
|
|
| 68 |
* Get the cache scope
|
|
| 69 |
*
|
|
| 70 |
* @return The cache scope
|
|
| 71 |
*/
|
|
| 72 | 0 |
public int getScope() { |
| 73 | 0 |
return scope;
|
| 74 |
} |
|
| 75 |
|
|
| 76 | 0 |
private void setScope(int scope) { |
| 77 | 0 |
this.scope = scope;
|
| 78 |
} |
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* When this Cache is bound to the session, do nothing.
|
|
| 82 |
*
|
|
| 83 |
* @param event The SessionBindingEvent.
|
|
| 84 |
*/
|
|
| 85 | 0 |
public void valueBound(HttpSessionBindingEvent event) { |
| 86 |
} |
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* When the users's session ends, all listeners are finalized and the
|
|
| 90 |
* session cache directory is deleted from disk.
|
|
| 91 |
*
|
|
| 92 |
* @param event The event that triggered this unbinding.
|
|
| 93 |
*/
|
|
| 94 | 0 |
public void valueUnbound(HttpSessionBindingEvent event) { |
| 95 | 0 |
if (log.isInfoEnabled()) {
|
| 96 | 0 |
log.info("[Cache] Unbound from session " + event.getSession().getId() + " using name " + event.getName()); |
| 97 |
} |
|
| 98 |
|
|
| 99 | 0 |
admin.finalizeListeners(this);
|
| 100 | 0 |
clear(); |
| 101 |
} |
|
| 102 |
|
|
| 103 |
/**
|
|
| 104 |
* Indicates whether or not the cache entry is stale. This overrides the
|
|
| 105 |
* {@link Cache#isStale(CacheEntry, int, String)} method to take into account any
|
|
| 106 |
* flushing that may have been applied to the scope that this cache belongs to.
|
|
| 107 |
*
|
|
| 108 |
* @param cacheEntry The cache entry to test the freshness of.
|
|
| 109 |
* @param refreshPeriod The maximum allowable age of the entry, in seconds.
|
|
| 110 |
* @param cronExpiry A cron expression that defines fixed expiry dates and/or
|
|
| 111 |
* times for this cache entry.
|
|
| 112 |
*
|
|
| 113 |
* @return <code>true</code> if the entry is stale, <code>false</code> otherwise.
|
|
| 114 |
*/
|
|
| 115 | 0 |
protected boolean isStale(CacheEntry cacheEntry, int refreshPeriod, String cronExpiry) { |
| 116 | 0 |
return super.isStale(cacheEntry, refreshPeriod, cronExpiry) || admin.isScopeFlushed(cacheEntry, scope); |
| 117 |
} |
|
| 118 |
} |
|
| 119 |
|
|
||||||||||