CacheManager.java 1.87 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2003 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */
package org.jivesoftware.util;

13
import java.util.*;
Matt Tucker's avatar
Matt Tucker committed
14 15

/**
16 17
 * A centralized, JVM static manager of Jive caches. Caches are essential for
 * scalability.
Matt Tucker's avatar
Matt Tucker committed
18 19 20 21 22
 *
 * @author Iain Shigeoka
 */
public class CacheManager {

23
    private static Map caches = new HashMap();
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28 29 30 31 32 33 34 35
    private static long maxLifetime = JiveConstants.HOUR * 6;

    /**
     * <p>Initialize a cache by name.</p>
     * <p/>
     * <p>Caches require initialization before use. Be careful to initialize your cache before using it.
     * Initializing a cache that has already been initialized once does nothing.</p>
     * <p/>
     * <p>The cache manager will check jive module context for overriding defaultMaxCacheSize values.
     * The property names should be "cache.name.size" where 'name' will be the same as the cache name.
     * If the property exists, that value will be used instead of the defaultMaxCacheSize.</p>
     *
36 37
     * @param name the name of the cache to create.
     * @param defaultMaxCacheSize the default max size the cache can grow to, in bytes.
Matt Tucker's avatar
Matt Tucker committed
38
     */
39
    public static void initializeCache(String name, int defaultMaxCacheSize) {
Matt Tucker's avatar
Matt Tucker committed
40 41
        Cache cache = (Cache)caches.get(name);
        if (cache == null) {
42
            int maxCacheSize = JiveGlobals.getIntProperty("cache." + name + ".size", defaultMaxCacheSize);
43
            caches.put(name, new Cache(name, maxCacheSize, maxLifetime));
Matt Tucker's avatar
Matt Tucker committed
44 45 46 47
        }
    }

    /**
48
     * Returns the cache specified by name.
Matt Tucker's avatar
Matt Tucker committed
49
     *
50 51
     * @param name the name of the cache to return.
     * @return the cache found, or null if no cache by that name has been initialized.
Matt Tucker's avatar
Matt Tucker committed
52 53 54 55
     */
    public static Cache getCache(String name) {
        return (Cache)caches.get(name);
    }
56
}