Ehcache.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /** Copyright 2009 by primeton Corporation.
  2. *
  3. * All rights reserved.
  4. *
  5. * This software is the confidential and proprietary information of
  6. * primeton Corporation ('Confidential Information'). You
  7. * shall not disclose such Confidential Information and shall use
  8. * it only in accordance with the terms of the license agreement
  9. * you entered into with primeton.
  10. */
  11. package com.yiidata.intergration.web.modules.sys.cache;
  12. import net.sf.ehcache.CacheManager;
  13. import net.sf.ehcache.Element;
  14. import net.sf.ehcache.search.Attribute;
  15. import net.sf.ehcache.search.Query;
  16. import net.sf.ehcache.search.Result;
  17. import net.sf.ehcache.search.Results;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import java.io.Closeable;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.util.List;
  24. /**
  25. * @author xiongbin
  26. * @Version 2012-9-4
  27. */
  28. public class Ehcache implements ICache<Object>, Closeable {
  29. private static Logger logger = LoggerFactory.getLogger(Ehcache.class);
  30. /**
  31. * EHCache
  32. */
  33. private final net.sf.ehcache.Ehcache cache;
  34. public Ehcache(){
  35. URL resource = Ehcache.class.getResource("/ehcache.xml");
  36. CacheManager manager = CacheManager.create(resource);
  37. net.sf.ehcache.Ehcache ehcache = manager.getCache("Cache");
  38. if(ehcache==null){
  39. manager.addCache("Cache");
  40. ehcache = manager.getEhcache("Cache");
  41. }
  42. this.cache = ehcache;
  43. }
  44. /* (non-Javadoc)
  45. * @see com.primeton.dgs.kernel.common.cache.ICache#add(java.lang.String, java.lang.Object)
  46. */
  47. @Override
  48. public void add(String key, Object value) {
  49. Element element = new Element(key, value);
  50. cache.put(element);
  51. }
  52. /* (non-Javadoc)
  53. * @see com.primeton.dgs.kernel.common.cache.ICache#add(java.lang.String, java.lang.Integer, java.lang.Object)
  54. */
  55. @Override
  56. public void add(String key, int exp, Object value) {
  57. Element element = new Element(key, value);
  58. element.setTimeToLive(exp);
  59. cache.put(element);
  60. }
  61. /* (non-Javadoc)
  62. * @see com.primeton.dgs.kernel.common.cache.ICache#get(java.lang.String)
  63. */
  64. @Override
  65. public Object get(String key) {
  66. Element element = cache.get(key);
  67. if(element != null){
  68. return element.getValue();
  69. }
  70. return null;
  71. }
  72. /**
  73. * 移除某条缓存
  74. * @param key 缓存的 KEY
  75. * @return
  76. */
  77. @Override
  78. public Object remove(String key) {
  79. Element element = cache.get(key);
  80. cache.remove(key);
  81. logger.info("remove cache key: {}", key);
  82. if(element != null){
  83. return element.getObjectValue();
  84. }
  85. return null;
  86. }
  87. @Override
  88. public int removeByPrefix(String prefix) {
  89. Attribute<Object> keySearch = cache.getSearchAttribute("key");
  90. //创建一个用于查询的Query对象
  91. Query query = cache.createQuery();
  92. //给当前query添加一个筛选条件——可查询属性name的值等于“name1”
  93. query.addCriteria(keySearch.ilike(prefix+"*"));
  94. // ehcache 模糊删除
  95. query.includeAttribute(keySearch);
  96. query.includeKeys();
  97. Results result = query.execute();
  98. List<Result> keys = result.all();
  99. for (Result key : keys) {
  100. remove((String)key.getKey());
  101. }
  102. return result.size();
  103. }
  104. /* (non-Javadoc)
  105. * @see com.primeton.dgs.kernel.common.cache.ICache#clear()
  106. */
  107. @Override
  108. public void clear() {
  109. cache.removeAll();
  110. }
  111. /**
  112. * 获得缓存队列的长度
  113. * @return 返回大于等于 0 则代表真实长度,返回 -1 代表无法获得
  114. */
  115. @Override public int size() {
  116. return cache.getSize();
  117. }
  118. @Override
  119. public void shutdown() {
  120. try {
  121. close();
  122. } catch (IOException e) {
  123. }
  124. }
  125. @Override
  126. public void close() throws IOException {
  127. cache.flush();
  128. }
  129. }