CacheManager.java 2.81 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4
/*
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 1999-2006 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
6 7 8
 *
 * Use is subject to license terms.
 */
9

Matt Tucker's avatar
Matt Tucker committed
10 11
package org.jivesoftware.util;

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

/**
15
 * Centralized management of caches. Caches are essential for performance and scalability.
Matt Tucker's avatar
Matt Tucker committed
16
 *
17 18
 * @see Cache
 * @author Matt Tucker
Matt Tucker's avatar
Matt Tucker committed
19 20 21
 */
public class CacheManager {

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    private static Map<String, Cache> caches = new HashMap<String, Cache>();
    private static final long DEFAULT_EXPIRATION_TIME = JiveConstants.HOUR * 6;

    /**
     * Initializes a cache given it's name and max size. The default expiration time
     * of six hours will be used. If a cache with the same name has already been initialized,
     * this method returns the existing cache.<p>
     *
     * The size and expiration time for the cache can be overridden by setting Jive properties
     * in the format:<ul>
     *
     *  <li>Size: "cache.CACHE_NAME.size", in bytes.
     *  <li>Expiration: "cache.CACHE_NAME.expirationTime", in milleseconds.
     * </ul>
     * where CACHE_NAME is the name of the cache.
     *
     * @param name the name of the cache to initialize.
     * @param size the size the cache can grow to, in bytes.
     */
    public static Cache initializeCache(String name, int size) {
        return initializeCache(name, size, DEFAULT_EXPIRATION_TIME);
    }
Matt Tucker's avatar
Matt Tucker committed
44 45

    /**
46 47 48 49 50 51 52 53 54 55
     * Initializes a cache given it's name, max size, and expiration time. If a cache with
     * the same name has already been initialized, this method returns the existing cache.<p>
     *
     * The size and expiration time for the cache can be overridden by setting Jive properties
     * in the format:<ul>
     *
     *  <li>Size: "cache.CACHE_NAME.size", in bytes.
     *  <li>Expiration: "cache.CACHE_NAME.expirationTime", in milleseconds.
     * </ul>
     * where CACHE_NAME is the name of the cache.
Matt Tucker's avatar
Matt Tucker committed
56
     *
57 58
     * @param name the name of the cache to initialize.
     * @param size the size the cache can grow to, in bytes.
Matt Tucker's avatar
Matt Tucker committed
59
     */
60 61
    public static Cache initializeCache(String name, int size, long expirationTime) {
        Cache cache = caches.get(name);
Matt Tucker's avatar
Matt Tucker committed
62
        if (cache == null) {
63 64 65 66
            size = JiveGlobals.getIntProperty("cache." + name + ".size", size);
            expirationTime = (long)JiveGlobals.getIntProperty("cache." + name + ".expirationTime",
                    (int)expirationTime);
            caches.put(name, new Cache(name, size, expirationTime));
Matt Tucker's avatar
Matt Tucker committed
67
        }
68
        return cache;
Matt Tucker's avatar
Matt Tucker committed
69 70 71
    }

    /**
72 73
     * Returns the cache specified by name. The cache must be initialized before this
     * method can be called.
Matt Tucker's avatar
Matt Tucker committed
74
     *
75
     * @param name the name of the cache to return.
76 77
     * @return the cache found, or <tt>null</tt> if no cache by that name
     *      has been initialized.
Matt Tucker's avatar
Matt Tucker committed
78 79
     */
    public static Cache getCache(String name) {
80
        return caches.get(name);
Matt Tucker's avatar
Matt Tucker committed
81
    }
82
}