PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/google/ie/common/cache/CacheHelper.java

http://thoughtsite.googlecode.com/
Java | 128 lines | 40 code | 14 blank | 74 comment | 4 complexity | 186a7a35c144837f3e74e183bdfa9202 MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.common.cache;
 17
 18import com.google.appengine.api.memcache.Expiration;
 19import com.google.appengine.api.memcache.MemcacheService;
 20import com.google.appengine.api.memcache.MemcacheServiceFactory;
 21
 22import java.io.Serializable;
 23
 24/**
 25 * A Utility class to manage cache.
 26 * 
 27 * @author asirohi
 28 * 
 29 */
 30public class CacheHelper {
 31    /* Default expiry time.Currently 5 minutes */
 32    private static final int DEFAULT_EXPIRY_TIME = 300;
 33    /**
 34     * Memcache service instance.
 35     */
 36    private static MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
 37
 38    /**
 39     * Fetch a previously-stored value, or null if not set.
 40     * 
 41     * @param nameSpace the namespace to be prefixed to the key
 42     * @param key the key object used to store the cache entry
 43     * @return an object stored with the given key.
 44     */
 45    public static Object getObject(String nameSpace, Serializable key) {
 46        key = prefixNamespaceToKey(nameSpace, key);
 47        return cache.get(key);
 48    }
 49
 50    /**
 51     * Store a new value into the cache using the key.
 52     * 
 53     * @param nameSpace the namespace to be prefixed to the key
 54     * @param key the key object used to store the new cache entry
 55     * @param value value for the new cache entry
 56     */
 57    public static void putObject(String nameSpace, Serializable key, Serializable value) {
 58        key = prefixNamespaceToKey(nameSpace, key);
 59        /* add default expiry time */
 60        cache.put(key, value, Expiration.byDeltaSeconds(DEFAULT_EXPIRY_TIME));
 61    }
 62
 63    /**
 64     * Store a new value into the cache using key along with the expiration
 65     * time.
 66     * 
 67     * @param nameSpace the namespace to be prefixed to the key
 68     * @param key the key object used to store the new cache entry
 69     * @param value value for the new cache entry
 70     * @param expirationDelay expire delay in seconds.
 71     */
 72    public static void putObject(String nameSpace, Serializable key, Serializable value,
 73                    int expirationDelay) {
 74        key = prefixNamespaceToKey(nameSpace, key);
 75        cache.put(key, value, Expiration.byDeltaSeconds(expirationDelay));
 76
 77    }
 78
 79    /**
 80     * Tests whether a given value is in cache, even if its value is null.
 81     * 
 82     * @param nameSpace the namespace to be prefixed to the key
 83     * @param key the key object used to store the cache entry
 84     * @return true if the cache contains an entry for the key
 85     */
 86    public static boolean containsObject(String nameSpace, Serializable key) {
 87        key = prefixNamespaceToKey(nameSpace, key);
 88        return cache.contains(key);
 89    }
 90
 91    /**
 92     * Removes key from the cache.
 93     * 
 94     * @param nameSpace the namespace to be prefixed to the key
 95     * @param key the key object used to store the cache entry
 96     */
 97    public static boolean deleteObject(String nameSpace, Serializable key) {
 98        key = prefixNamespaceToKey(nameSpace, key);
 99        return cache.delete(key);
100    }
101
102    /**
103     * Empties the cache of all values. Statistics are not affected.
104     * Note that clearAll() does not respect namespaces - this flushes the cache
105     * for every namespace.
106     */
107    public static void clearAllObjects() {
108        cache.clearAll();
109    }
110
111    /**
112     * Prefixes the namespace to the key.
113     * 
114     * @param nameSpace the namespace to be prefixed to the key
115     * @param key the key object to which the namespace is to be prefixed
116     * @return the key prefixed with the namespace with an underscore joining
117     *         them
118     */
119    private static Serializable prefixNamespaceToKey(String nameSpace, Serializable key) {
120        if (null == nameSpace || null == key) {
121            throw new IllegalArgumentException(CacheConstants.NAMESPACE_OR_KEY_IS_NULL);
122        }
123        key = nameSpace + CacheConstants.UNDERSCORE + key;
124        return key;
125    }
126
127}
128