|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| GeneralCacheAdministrator.java | - | 68% | 63,2% | 65,9% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (c) 2002-2003 by OpenSymphony
|
|
| 3 |
* All rights reserved.
|
|
| 4 |
*/
|
|
| 5 |
package com.opensymphony.oscache.general;
|
|
| 6 |
|
|
| 7 |
import com.opensymphony.oscache.base.*;
|
|
| 8 |
|
|
| 9 |
import org.apache.commons.logging.Log;
|
|
| 10 |
import org.apache.commons.logging.LogFactory;
|
|
| 11 |
|
|
| 12 |
import java.util.Date;
|
|
| 13 |
import java.util.Properties;
|
|
| 14 |
|
|
| 15 |
/**
|
|
| 16 |
* A GeneralCacheAdministrator creates, flushes and administers the cache.
|
|
| 17 |
*
|
|
| 18 |
* EXAMPLES :
|
|
| 19 |
* <pre><code>
|
|
| 20 |
* // ---------------------------------------------------------------
|
|
| 21 |
* // Typical use with fail over
|
|
| 22 |
* // ---------------------------------------------------------------
|
|
| 23 |
* String myKey = "myKey";
|
|
| 24 |
* String myValue;
|
|
| 25 |
* int myRefreshPeriod = 1000;
|
|
| 26 |
* try {
|
|
| 27 |
* // Get from the cache
|
|
| 28 |
* myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
|
|
| 29 |
* } catch (NeedsRefreshException nre) {
|
|
| 30 |
* try {
|
|
| 31 |
* // Get the value (probably by calling an EJB)
|
|
| 32 |
* myValue = "This is the content retrieved.";
|
|
| 33 |
* // Store in the cache
|
|
| 34 |
* admin.putInCache(myKey, myValue);
|
|
| 35 |
* } catch (Exception ex) {
|
|
| 36 |
* // We have the current content if we want fail-over.
|
|
| 37 |
* myValue = (String) nre.getCacheContent();
|
|
| 38 |
* // It is essential that cancelUpdate is called if the
|
|
| 39 |
* // cached content is not rebuilt
|
|
| 40 |
* admin.cancelUpdate(myKey);
|
|
| 41 |
* }
|
|
| 42 |
* }
|
|
| 43 |
*
|
|
| 44 |
*
|
|
| 45 |
*
|
|
| 46 |
* // ---------------------------------------------------------------
|
|
| 47 |
* // Typical use without fail over
|
|
| 48 |
* // ---------------------------------------------------------------
|
|
| 49 |
* String myKey = "myKey";
|
|
| 50 |
* String myValue;
|
|
| 51 |
* int myRefreshPeriod = 1000;
|
|
| 52 |
* try {
|
|
| 53 |
* // Get from the cache
|
|
| 54 |
* myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
|
|
| 55 |
* } catch (NeedsRefreshException nre) {
|
|
| 56 |
* try {
|
|
| 57 |
* // Get the value (probably by calling an EJB)
|
|
| 58 |
* myValue = "This is the content retrieved.";
|
|
| 59 |
* // Store in the cache
|
|
| 60 |
* admin.putInCache(myKey, myValue);
|
|
| 61 |
* updated = true;
|
|
| 62 |
* } finally {
|
|
| 63 |
* if (!updated) {
|
|
| 64 |
* // It is essential that cancelUpdate is called if the
|
|
| 65 |
* // cached content could not be rebuilt
|
|
| 66 |
* admin.cancelUpdate(myKey);
|
|
| 67 |
* }
|
|
| 68 |
* }
|
|
| 69 |
* }
|
|
| 70 |
* // ---------------------------------------------------------------
|
|
| 71 |
* // ---------------------------------------------------------------
|
|
| 72 |
* </code></pre>
|
|
| 73 |
*
|
|
| 74 |
* @version $Revision: 1.8 $
|
|
| 75 |
* @author <a href="mailto:fbeauregard@pyxis-tech.com">Francois Beauregard</a>
|
|
| 76 |
* @author <a href="mailto:abergevin@pyxis-tech.com">Alain Bergevin</a>
|
|
| 77 |
*/
|
|
| 78 |
public class GeneralCacheAdministrator extends AbstractCacheAdministrator { |
|
| 79 |
private static transient final Log log = LogFactory.getLog(GeneralCacheAdministrator.class); |
|
| 80 |
|
|
| 81 |
/**
|
|
| 82 |
* Application cache
|
|
| 83 |
*/
|
|
| 84 |
private Cache applicationCache = null; |
|
| 85 |
|
|
| 86 |
/**
|
|
| 87 |
* Create the cache administrator.
|
|
| 88 |
*/
|
|
| 89 | 51 |
public GeneralCacheAdministrator() {
|
| 90 | 51 |
this(null); |
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Create the cache administrator with the specified properties
|
|
| 95 |
*/
|
|
| 96 | 54 |
public GeneralCacheAdministrator(Properties p) {
|
| 97 | 54 |
super(p);
|
| 98 | 54 |
log.info("Constructed GeneralCacheAdministrator()");
|
| 99 | 54 |
createCache(); |
| 100 |
} |
|
| 101 |
|
|
| 102 |
/**
|
|
| 103 |
* Grabs a cache
|
|
| 104 |
*
|
|
| 105 |
* @return The cache
|
|
| 106 |
*/
|
|
| 107 | 729 |
public Cache getCache() {
|
| 108 | 729 |
return applicationCache;
|
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* Get an object from the cache
|
|
| 113 |
*
|
|
| 114 |
* @param key The key entered by the user.
|
|
| 115 |
* @return The object from cache
|
|
| 116 |
* @throws NeedsRefreshException when no cache entry could be found with the
|
|
| 117 |
* supplied key, or when an entry was found but is considered out of date. If
|
|
| 118 |
* the cache entry is a new entry that is currently being constructed this method
|
|
| 119 |
* will block until the new entry becomes available. Similarly, it will block if
|
|
| 120 |
* a stale entry is currently being rebuilt by another thread and cache blocking is
|
|
| 121 |
* enabled (<code>cache.blocking=true</code>).
|
|
| 122 |
*/
|
|
| 123 | 0 |
public Object getFromCache(String key) throws NeedsRefreshException { |
| 124 | 0 |
return getCache().getFromCache(key);
|
| 125 |
} |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Get an object from the cache
|
|
| 129 |
*
|
|
| 130 |
* @param key The key entered by the user.
|
|
| 131 |
* @param refreshPeriod How long the object can stay in cache in seconds. To
|
|
| 132 |
* allow the entry to stay in the cache indefinitely, supply a value of
|
|
| 133 |
* {@link CacheEntry#INDEFINITE_EXPIRY}
|
|
| 134 |
* @return The object from cache
|
|
| 135 |
* @throws NeedsRefreshException when no cache entry could be found with the
|
|
| 136 |
* supplied key, or when an entry was found but is considered out of date. If
|
|
| 137 |
* the cache entry is a new entry that is currently being constructed this method
|
|
| 138 |
* will block until the new entry becomes available. Similarly, it will block if
|
|
| 139 |
* a stale entry is currently being rebuilt by another thread and cache blocking is
|
|
| 140 |
* enabled (<code>cache.blocking=true</code>).
|
|
| 141 |
*/
|
|
| 142 | 270 |
public Object getFromCache(String key, int refreshPeriod) throws NeedsRefreshException { |
| 143 | 270 |
return getCache().getFromCache(key, refreshPeriod);
|
| 144 |
} |
|
| 145 |
|
|
| 146 |
/**
|
|
| 147 |
* Get an object from the cache
|
|
| 148 |
*
|
|
| 149 |
* @param key The key entered by the user.
|
|
| 150 |
* @param refreshPeriod How long the object can stay in cache in seconds. To
|
|
| 151 |
* allow the entry to stay in the cache indefinitely, supply a value of
|
|
| 152 |
* {@link CacheEntry#INDEFINITE_EXPIRY}
|
|
| 153 |
* @param cronExpression A cron expression that the age of the cache entry
|
|
| 154 |
* will be compared to. If the entry is older than the most recent match for the
|
|
| 155 |
* cron expression, the entry will be considered stale.
|
|
| 156 |
* @return The object from cache
|
|
| 157 |
* @throws NeedsRefreshException when no cache entry could be found with the
|
|
| 158 |
* supplied key, or when an entry was found but is considered out of date. If
|
|
| 159 |
* the cache entry is a new entry that is currently being constructed this method
|
|
| 160 |
* will block until the new entry becomes available. Similarly, it will block if
|
|
| 161 |
* a stale entry is currently being rebuilt by another thread and cache blocking is
|
|
| 162 |
* enabled (<code>cache.blocking=true</code>).
|
|
| 163 |
*/
|
|
| 164 | 0 |
public Object getFromCache(String key, int refreshPeriod, String cronExpression) throws NeedsRefreshException { |
| 165 | 0 |
return getCache().getFromCache(key, refreshPeriod, cronExpression);
|
| 166 |
} |
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* Cancels a pending cache update. This should only be called by a thread
|
|
| 170 |
* that received a {@link NeedsRefreshException} and was unable to generate
|
|
| 171 |
* some new cache content.
|
|
| 172 |
*
|
|
| 173 |
* @param key The cache entry key to cancel the update of.
|
|
| 174 |
*/
|
|
| 175 | 75 |
public void cancelUpdate(String key) { |
| 176 | 75 |
getCache().cancelUpdate(key); |
| 177 |
} |
|
| 178 |
|
|
| 179 |
/**
|
|
| 180 |
* Shuts down the cache administrator.
|
|
| 181 |
*/
|
|
| 182 | 3 |
public void destroy() { |
| 183 | 3 |
finalizeListeners(applicationCache); |
| 184 |
} |
|
| 185 |
|
|
| 186 |
// METHODS THAT DELEGATES TO THE CACHE ---------------------
|
|
| 187 |
|
|
| 188 |
/**
|
|
| 189 |
* Flush the entire cache immediately.
|
|
| 190 |
*/
|
|
| 191 | 0 |
public void flushAll() { |
| 192 | 0 |
getCache().flushAll(new Date());
|
| 193 |
} |
|
| 194 |
|
|
| 195 |
/**
|
|
| 196 |
* Flush the entire cache at the given date.
|
|
| 197 |
*
|
|
| 198 |
* @param date The time to flush
|
|
| 199 |
*/
|
|
| 200 | 0 |
public void flushAll(Date date) { |
| 201 | 0 |
getCache().flushAll(date); |
| 202 |
} |
|
| 203 |
|
|
| 204 |
/**
|
|
| 205 |
* Flushes a single cache entry.
|
|
| 206 |
*/
|
|
| 207 | 0 |
public void flushEntry(String key) { |
| 208 | 0 |
getCache().flushEntry(key); |
| 209 |
} |
|
| 210 |
|
|
| 211 |
/**
|
|
| 212 |
* Flushes all items that belong to the specified group.
|
|
| 213 |
*
|
|
| 214 |
* @param group The name of the group to flush
|
|
| 215 |
*/
|
|
| 216 | 24 |
public void flushGroup(String group) { |
| 217 | 24 |
getCache().flushGroup(group); |
| 218 |
} |
|
| 219 |
|
|
| 220 |
/**
|
|
| 221 |
* Allows to flush all items that have a specified pattern in the key.
|
|
| 222 |
*
|
|
| 223 |
* @param pattern Pattern.
|
|
| 224 |
* @deprecated For performance and flexibility reasons it is preferable to
|
|
| 225 |
* store cache entries in groups and use the {@link #flushGroup(String)} method
|
|
| 226 |
* instead of relying on pattern flushing.
|
|
| 227 |
*/
|
|
| 228 | 18 |
public void flushPattern(String pattern) { |
| 229 | 18 |
getCache().flushPattern(pattern); |
| 230 |
} |
|
| 231 |
|
|
| 232 |
/**
|
|
| 233 |
* Put an object in a cache
|
|
| 234 |
*
|
|
| 235 |
* @param key The key entered by the user
|
|
| 236 |
* @param content The object to store
|
|
| 237 |
* @param policy Object that implements refresh policy logic
|
|
| 238 |
*/
|
|
| 239 | 141 |
public void putInCache(String key, Object content, EntryRefreshPolicy policy) { |
| 240 | 141 |
Cache cache = getCache(); |
| 241 | 141 |
cache.putInCache(key, content, policy); |
| 242 |
} |
|
| 243 |
|
|
| 244 |
/**
|
|
| 245 |
* Put an object in a cache
|
|
| 246 |
*
|
|
| 247 |
* @param key The key entered by the user
|
|
| 248 |
* @param content The object to store
|
|
| 249 |
*/
|
|
| 250 | 138 |
public void putInCache(String key, Object content) { |
| 251 | 138 |
putInCache(key, content, (EntryRefreshPolicy) null);
|
| 252 |
} |
|
| 253 |
|
|
| 254 |
/**
|
|
| 255 |
* Puts an object in a cache
|
|
| 256 |
*
|
|
| 257 |
* @param key The unique key for this cached object
|
|
| 258 |
* @param content The object to store
|
|
| 259 |
* @param groups The groups that this object belongs to
|
|
| 260 |
*/
|
|
| 261 | 54 |
public void putInCache(String key, Object content, String[] groups) { |
| 262 | 54 |
getCache().putInCache(key, content, groups); |
| 263 |
} |
|
| 264 |
|
|
| 265 |
/**
|
|
| 266 |
* Puts an object in a cache
|
|
| 267 |
*
|
|
| 268 |
* @param key The unique key for this cached object
|
|
| 269 |
* @param content The object to store
|
|
| 270 |
* @param groups The groups that this object belongs to
|
|
| 271 |
* @param policy The refresh policy to use
|
|
| 272 |
*/
|
|
| 273 | 0 |
public void putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy) { |
| 274 | 0 |
getCache().putInCache(key, content, groups, policy, null);
|
| 275 |
} |
|
| 276 |
|
|
| 277 |
/**
|
|
| 278 |
* Sets the cache capacity (number of items). If the cache contains
|
|
| 279 |
* more than <code>capacity</code> items then items will be removed
|
|
| 280 |
* to bring the cache back down to the new size.
|
|
| 281 |
*
|
|
| 282 |
* @param capacity The new capacity of the cache
|
|
| 283 |
*/
|
|
| 284 | 0 |
public void setCacheCapacity(int capacity) { |
| 285 | 0 |
super.setCacheCapacity(capacity);
|
| 286 | 0 |
getCache().setCapacity(capacity); |
| 287 |
} |
|
| 288 |
|
|
| 289 |
/**
|
|
| 290 |
* Creates a cache in this admin
|
|
| 291 |
*/
|
|
| 292 | 54 |
private void createCache() { |
| 293 | 54 |
log.info("Creating new cache");
|
| 294 |
|
|
| 295 | 54 |
applicationCache = new Cache(isMemoryCaching(), isUnlimitedDiskCache(), isBlocking(), algorithmClass, cacheCapacity);
|
| 296 |
|
|
| 297 | 54 |
configureStandardListeners(applicationCache); |
| 298 |
} |
|
| 299 |
} |
|
| 300 |
|
|
||||||||||