KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > CacheManager


1 /*
2  * $RCSfile: CacheManager.java,v $
3  * $Revision: 1.4 $
4  * $Date: 2005/04/11 21:05:19 $
5  *
6  * Copyright (C) 1999-2003 CoolServlets, Inc. All rights reserved.
7  *
8  * This software is the proprietary information of CoolServlets, Inc.
9  * Use is subject to license terms.
10  */

11 package org.jivesoftware.util;
12
13 import java.util.*;
14
15 /**
16  * A centralized, JVM static manager of Jive caches. Caches are essential for
17  * scalability.
18  *
19  * @author Iain Shigeoka
20  */

21 public class CacheManager {
22
23     private static Map caches = new HashMap();
24     private static long maxLifetime = JiveConstants.HOUR * 6;
25
26     /**
27      * <p>Initialize a cache by name.</p>
28      * <p/>
29      * <p>Caches require initialization before use. Be careful to initialize your cache before using it.
30      * Initializing a cache that has already been initialized once does nothing.</p>
31      * <p/>
32      * <p>The cache manager will check jive module context for overriding defaultMaxCacheSize values.
33      * The property names should be "cache.name.size" where 'name' will be the same as the cache name.
34      * If the property exists, that value will be used instead of the defaultMaxCacheSize.</p>
35      *
36      * @param name the name of the cache to create.
37      * @param defaultMaxCacheSize the default max size the cache can grow to, in bytes.
38      */

39     public static void initializeCache(String JavaDoc name, int defaultMaxCacheSize) {
40         Cache cache = (Cache)caches.get(name);
41         if (cache == null) {
42             int maxCacheSize = JiveGlobals.getIntProperty("cache." + name + ".size", defaultMaxCacheSize);
43             caches.put(name, new Cache(name, maxCacheSize, maxLifetime));
44         }
45     }
46
47     /**
48      * Returns the cache specified by name.
49      *
50      * @param name the name of the cache to return.
51      * @return the cache found, or null if no cache by that name has been initialized.
52      */

53     public static Cache getCache(String JavaDoc name) {
54         return (Cache)caches.get(name);
55     }
56 }
Popular Tags