EhCacheEngine.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /******************************************************************************
  2.  * Sony Online Entertainment
  3.  * Application Engineering
  4.  *
  5.  * Unpublished work Copyright 2005 Sony Online Entertainment Inc.
  6.  * All rights reserved.
  7.  * Created on Oct 11, 2005
  8.  ******************************************************************************/
  9. package net.jforum.cache;
  10. import java.io.Serializable;
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import net.jforum.util.preferences.SystemGlobals;
  16. import net.sf.ehcache.Cache;
  17. import net.sf.ehcache.CacheException;
  18. import net.sf.ehcache.CacheManager;
  19. import net.sf.ehcache.Element;
  20. import org.apache.log4j.Logger;
  21. /**
  22.  * The rest of the application seems to make some invalid assumptions about how
  23.  * things are cached.  Those assumptions might be benign, but it is hard to tell
  24.  * without deep testing.  Until this is finishe the JBossCacheEngine should be 
  25.  * configured in a local mode.
  26.  *
  27.  * Created on Oct 11, 2005 
  28.  *
  29.  * @author Jake Fear
  30.  * @version $Id: EhCacheEngine.java,v 1.1 2005/10/14 00:15:54 rafaelsteil Exp $
  31.  */
  32. public class EhCacheEngine implements CacheEngine {
  33. private static final Logger log = Logger.getLogger(EhCacheEngine.class);
  34. private CacheManager manager;
  35. public void init() {
  36. try {
  37. manager = CacheManager.create(SystemGlobals.getValue("ehcache.cache.properties"));
  38. } catch (CacheException ce) {
  39. log.error("EhCache could not be initialized", ce);
  40. throw new RuntimeException(ce);
  41. }
  42. }
  43. public void stop() {
  44. manager.shutdown();
  45. }
  46. public void add(String key, Object value) {
  47. if (log.isDebugEnabled()) {
  48. log.debug("Caching " + value + " with key " + key);
  49. }
  50. add(DUMMY_FQN, key, value);
  51. }
  52. public void add(String fullyQualifiedName, String key, Object value) {
  53. if (!manager.cacheExists(fullyQualifiedName)) {
  54. try {
  55. manager.addCache(fullyQualifiedName);
  56. } catch (CacheException ce) {
  57. log.error(ce, ce);
  58. throw new RuntimeException(ce);
  59. }
  60. }
  61. Cache cache = manager.getCache(fullyQualifiedName);
  62. Element element = new Element(key, (Serializable)value);
  63. cache.put(element);
  64. }
  65. public Object get(String fullyQualifiedName, String key) {
  66. try {
  67. if (!manager.cacheExists(fullyQualifiedName)) {
  68. manager.addCache(fullyQualifiedName);
  69. return null;
  70. }
  71. Cache cache = manager.getCache(fullyQualifiedName);
  72. Element element = cache.get(key);
  73. if (element != null) {
  74. return element.getValue();
  75. return null;
  76. } catch (CacheException ce) {
  77. log.error("EhCache could not be shutdown", ce);
  78. throw new RuntimeException(ce);
  79. }
  80. }
  81. public Object get(String fullyQualifiedName) {
  82. if (!manager.cacheExists(fullyQualifiedName)) {
  83. try {
  84. manager.addCache(fullyQualifiedName);
  85. } catch (CacheException ce) {
  86. log.error("EhCache could not be shutdown", ce);
  87. throw new RuntimeException(ce);
  88. }
  89. }
  90. Cache cache = manager.getCache(fullyQualifiedName);
  91. return cache;
  92. }
  93. public Collection getValues(String fullyQualifiedName) {
  94. try {
  95. if (!manager.cacheExists(fullyQualifiedName)) {
  96. manager.addCache(fullyQualifiedName);
  97. return new ArrayList();
  98. }
  99. Cache cache = manager.getCache(fullyQualifiedName);
  100. List values = new ArrayList(cache.getSize());
  101. List keys = cache.getKeys();
  102. for (Iterator iter = keys.iterator(); iter.hasNext(); ) {
  103. values.add(cache.get((Serializable)iter.next()));
  104. }
  105. return values;
  106. } catch (CacheException ce) {
  107. log.error("EhCache could not be shutdown", ce);
  108. throw new RuntimeException(ce);
  109. }
  110. }
  111. public void remove(String fullyQualifiedName, String key) {
  112. Cache cache = manager.getCache(fullyQualifiedName);
  113. if (cache != null) {
  114. cache.remove(key);
  115. }
  116. }
  117. public void remove(String fullyQualifiedName) {
  118. if (manager.cacheExists(fullyQualifiedName)) {
  119. manager.removeCache(fullyQualifiedName);
  120. }
  121. }
  122. }