/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. package com.google.ie.common.cache;
  16. import com.google.appengine.api.memcache.Expiration;
  17. import com.google.appengine.api.memcache.MemcacheService;
  18. import com.google.appengine.api.memcache.MemcacheServiceFactory;
  19. import java.io.Serializable;
  20. /**
  21. * A Utility class to manage cache.
  22. *
  23. * @author asirohi
  24. *
  25. */
  26. public class CacheHelper {
  27. /* Default expiry time.Currently 5 minutes */
  28. private static final int DEFAULT_EXPIRY_TIME = 300;
  29. /**
  30. * Memcache service instance.
  31. */
  32. private static MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
  33. /**
  34. * Fetch a previously-stored value, or null if not set.
  35. *
  36. * @param nameSpace the namespace to be prefixed to the key
  37. * @param key the key object used to store the cache entry
  38. * @return an object stored with the given key.
  39. */
  40. public static Object getObject(String nameSpace, Serializable key) {
  41. key = prefixNamespaceToKey(nameSpace, key);
  42. return cache.get(key);
  43. }
  44. /**
  45. * Store a new value into the cache using the key.
  46. *
  47. * @param nameSpace the namespace to be prefixed to the key
  48. * @param key the key object used to store the new cache entry
  49. * @param value value for the new cache entry
  50. */
  51. public static void putObject(String nameSpace, Serializable key, Serializable value) {
  52. key = prefixNamespaceToKey(nameSpace, key);
  53. /* add default expiry time */
  54. cache.put(key, value, Expiration.byDeltaSeconds(DEFAULT_EXPIRY_TIME));
  55. }
  56. /**
  57. * Store a new value into the cache using key along with the expiration
  58. * time.
  59. *
  60. * @param nameSpace the namespace to be prefixed to the key
  61. * @param key the key object used to store the new cache entry
  62. * @param value value for the new cache entry
  63. * @param expirationDelay expire delay in seconds.
  64. */
  65. public static void putObject(String nameSpace, Serializable key, Serializable value,
  66. int expirationDelay) {
  67. key = prefixNamespaceToKey(nameSpace, key);
  68. cache.put(key, value, Expiration.byDeltaSeconds(expirationDelay));
  69. }
  70. /**
  71. * Tests whether a given value is in cache, even if its value is null.
  72. *
  73. * @param nameSpace the namespace to be prefixed to the key
  74. * @param key the key object used to store the cache entry
  75. * @return true if the cache contains an entry for the key
  76. */
  77. public static boolean containsObject(String nameSpace, Serializable key) {
  78. key = prefixNamespaceToKey(nameSpace, key);
  79. return cache.contains(key);
  80. }
  81. /**
  82. * Removes key from the cache.
  83. *
  84. * @param nameSpace the namespace to be prefixed to the key
  85. * @param key the key object used to store the cache entry
  86. */
  87. public static boolean deleteObject(String nameSpace, Serializable key) {
  88. key = prefixNamespaceToKey(nameSpace, key);
  89. return cache.delete(key);
  90. }
  91. /**
  92. * Empties the cache of all values. Statistics are not affected.
  93. * Note that clearAll() does not respect namespaces - this flushes the cache
  94. * for every namespace.
  95. */
  96. public static void clearAllObjects() {
  97. cache.clearAll();
  98. }
  99. /**
  100. * Prefixes the namespace to the key.
  101. *
  102. * @param nameSpace the namespace to be prefixed to the key
  103. * @param key the key object to which the namespace is to be prefixed
  104. * @return the key prefixed with the namespace with an underscore joining
  105. * them
  106. */
  107. private static Serializable prefixNamespaceToKey(String nameSpace, Serializable key) {
  108. if (null == nameSpace || null == key) {
  109. throw new IllegalArgumentException(CacheConstants.NAMESPACE_OR_KEY_IS_NULL);
  110. }
  111. key = nameSpace + CacheConstants.UNDERSCORE + key;
  112. return key;
  113. }
  114. }