|
| 1 | +package org.apache.jcs.engine.memory; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.Serializable; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Hashtable; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.commons.logging.Log; |
| 11 | +import org.apache.commons.logging.LogFactory; |
| 12 | +import org.apache.jcs.engine.CacheConstants; |
| 13 | +import org.apache.jcs.engine.CacheElement; |
| 14 | +import org.apache.jcs.engine.behavior.ICacheElement; |
| 15 | +import org.apache.jcs.engine.behavior.ICompositeCacheAttributes; |
| 16 | +import org.apache.jcs.engine.behavior.IElementAttributes; |
| 17 | +import org.apache.jcs.engine.control.CompositeCache; |
| 18 | +import org.apache.jcs.engine.memory.MemoryCache; |
| 19 | +import org.apache.jcs.engine.memory.shrinking.ShrinkerThread; |
| 20 | +import org.apache.jcs.engine.control.group.GroupId; |
| 21 | +import org.apache.jcs.engine.control.group.GroupAttrName; |
| 22 | + |
| 23 | +/** |
| 24 | + * Some common code for the LRU and MRU caches. |
| 25 | + * |
| 26 | + *@author <a href="mailto:asmuts@yahoo.com">Aaron Smuts</a> |
| 27 | + *@author <a href="mailto:jtaylor@apache.org">James Taylor</a> |
| 28 | + *@author <a href="mailto:jmcnally@apache.org">John McNally</a> |
| 29 | + *@created May 13, 2002 |
| 30 | + *@version $Id$ |
| 31 | + */ |
| 32 | +public abstract class AbstractMemoryCache |
| 33 | + implements MemoryCache, Serializable |
| 34 | +{ |
| 35 | + private final static Log log = |
| 36 | + LogFactory.getLog( AbstractMemoryCache.class ); |
| 37 | + |
| 38 | + protected String cacheName; |
| 39 | + |
| 40 | + /** |
| 41 | + * Map where items are stored by key |
| 42 | + */ |
| 43 | + protected Map map; |
| 44 | + |
| 45 | + /** |
| 46 | + * Region Elemental Attributes, used as a default. |
| 47 | + */ |
| 48 | + public IElementAttributes attr; |
| 49 | + |
| 50 | + /** |
| 51 | + * Cache Attributes |
| 52 | + */ |
| 53 | + public ICompositeCacheAttributes cattr; |
| 54 | + |
| 55 | + /** |
| 56 | + * The cache region this store is associated with |
| 57 | + */ |
| 58 | + protected CompositeCache cache; |
| 59 | + |
| 60 | + // status |
| 61 | + protected int status; |
| 62 | + |
| 63 | + // make configurable |
| 64 | + protected int chunkSize = 2; |
| 65 | + |
| 66 | + /** |
| 67 | + * The background memory shrinker |
| 68 | + */ |
| 69 | + private ShrinkerThread shrinker; |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Constructor for the LRUMemoryCache object |
| 74 | + */ |
| 75 | + public AbstractMemoryCache() |
| 76 | + { |
| 77 | + status = CacheConstants.STATUS_ERROR; |
| 78 | + map = new Hashtable(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * For post reflection creation initialization |
| 83 | + * |
| 84 | + *@param hub |
| 85 | + */ |
| 86 | + public synchronized void initialize( CompositeCache hub ) |
| 87 | + { |
| 88 | + this.cacheName = hub.getCacheName(); |
| 89 | + this.cattr = hub.getCacheAttributes(); |
| 90 | + this.cache = hub; |
| 91 | + |
| 92 | + status = CacheConstants.STATUS_ALIVE; |
| 93 | + |
| 94 | + if ( cattr.getUseMemoryShrinker() && shrinker == null ) |
| 95 | + { |
| 96 | + shrinker = new ShrinkerThread( this ); |
| 97 | + shrinker.setPriority( shrinker.MIN_PRIORITY ); |
| 98 | + shrinker.start(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Removes an item from the cache |
| 104 | + * |
| 105 | + *@param key Identifies item to be removed |
| 106 | + *@return Description of the Return Value |
| 107 | + *@exception IOException Description of the Exception |
| 108 | + */ |
| 109 | + public abstract boolean remove( Serializable key ) |
| 110 | + throws IOException; |
| 111 | + |
| 112 | + /** |
| 113 | + * Get an item from the cache |
| 114 | + * |
| 115 | + *@param key Description of the Parameter |
| 116 | + *@return Description of the Return Value |
| 117 | + *@exception IOException Description of the Exception |
| 118 | + */ |
| 119 | + public abstract ICacheElement get( Serializable key ) |
| 120 | + throws IOException; |
| 121 | + |
| 122 | + /** |
| 123 | + * Get an item from the cache without effecting its order or last access |
| 124 | + * time |
| 125 | + * |
| 126 | + *@param key Description of the Parameter |
| 127 | + *@return The quiet value |
| 128 | + *@exception IOException Description of the Exception |
| 129 | + */ |
| 130 | + public abstract ICacheElement getQuiet( Serializable key ) |
| 131 | + throws IOException; |
| 132 | + |
| 133 | + /** |
| 134 | + * Puts an item to the cache. |
| 135 | + * |
| 136 | + *@param ce Description of the Parameter |
| 137 | + *@exception IOException Description of the Exception |
| 138 | + */ |
| 139 | + public abstract void update( ICacheElement ce ) |
| 140 | + throws IOException; |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * Get an Array of the keys for all elements in the memory cache |
| 145 | + * |
| 146 | + *@return An Object[] |
| 147 | + */ |
| 148 | + public abstract Object[] getKeyArray(); |
| 149 | + |
| 150 | + |
| 151 | + /** |
| 152 | + * Removes all cached items from the cache. |
| 153 | + * |
| 154 | + * @exception IOException |
| 155 | + */ |
| 156 | + public void removeAll() |
| 157 | + throws IOException |
| 158 | + { |
| 159 | + map = new HashMap(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Prepares for shutdown. |
| 164 | + * |
| 165 | + * @exception IOException |
| 166 | + */ |
| 167 | + public void dispose() |
| 168 | + throws IOException |
| 169 | + { |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Returns the cache statistics. |
| 174 | + * |
| 175 | + * @return The stats value |
| 176 | + */ |
| 177 | + public String getStats() |
| 178 | + { |
| 179 | + return ""; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Returns the current cache size. |
| 184 | + * |
| 185 | + * @return The size value |
| 186 | + */ |
| 187 | + public int getSize() |
| 188 | + { |
| 189 | + return this.map.size(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Returns the cache status. |
| 194 | + * |
| 195 | + * @return The status value |
| 196 | + */ |
| 197 | + public int getStatus() |
| 198 | + { |
| 199 | + return this.status; |
| 200 | + //return this.STATUS_ALIVE; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Returns the cache name. |
| 205 | + * |
| 206 | + * @return The cacheName value |
| 207 | + */ |
| 208 | + public String getCacheName() |
| 209 | + { |
| 210 | + return this.cattr.getCacheName(); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Puts an item to the cache. |
| 215 | + * |
| 216 | + * @param me |
| 217 | + * @exception IOException |
| 218 | + */ |
| 219 | + public void waterfal( ICacheElement ce ) |
| 220 | + throws IOException |
| 221 | + { |
| 222 | + this.cache.spoolToDisk( ce ); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Gets the iterator attribute of the LRUMemoryCache object |
| 227 | + * |
| 228 | + * @return The iterator value |
| 229 | + */ |
| 230 | + public Iterator getIterator() |
| 231 | + { |
| 232 | + return map.entrySet().iterator(); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Returns the CacheAttributes. |
| 237 | + * |
| 238 | + * @return The CacheAttributes value |
| 239 | + */ |
| 240 | + public ICompositeCacheAttributes getCacheAttributes() |
| 241 | + { |
| 242 | + return this.cattr; |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Sets the CacheAttributes. |
| 247 | + * |
| 248 | + * @param cattr The new CacheAttributes value |
| 249 | + */ |
| 250 | + public void setCacheAttributes( ICompositeCacheAttributes cattr ) |
| 251 | + { |
| 252 | + this.cattr = cattr; |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Gets the cache hub / region taht the MemoryCache is used by |
| 257 | + * |
| 258 | + *@return The cache value |
| 259 | + */ |
| 260 | + public CompositeCache getCompositeCache() |
| 261 | + { |
| 262 | + return this.cache; |
| 263 | + } |
| 264 | +} |
0 commit comments