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