JedisCacheManager.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
  3. */
  4. package com.jeeplus.common.security.shiro.cache;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Set;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.shiro.cache.Cache;
  10. import org.apache.shiro.cache.CacheException;
  11. import org.apache.shiro.cache.CacheManager;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import redis.clients.jedis.Jedis;
  15. import com.google.common.collect.Sets;
  16. import com.jeeplus.common.utils.JedisUtils;
  17. import com.jeeplus.common.web.Servlets;
  18. /**
  19. * 自定义授权缓存管理类
  20. * @author jeeplus
  21. * @version 2014-7-20
  22. */
  23. public class JedisCacheManager implements CacheManager {
  24. private String cacheKeyPrefix = "shiro_cache_";
  25. @Override
  26. public <K, V> Cache<K, V> getCache(String name) throws CacheException {
  27. return new JedisCache<K, V>(cacheKeyPrefix + name);
  28. }
  29. public String getCacheKeyPrefix() {
  30. return cacheKeyPrefix;
  31. }
  32. public void setCacheKeyPrefix(String cacheKeyPrefix) {
  33. this.cacheKeyPrefix = cacheKeyPrefix;
  34. }
  35. /**
  36. * 自定义授权缓存管理类
  37. * @author jeeplus
  38. * @version 2014-7-20
  39. */
  40. public class JedisCache<K, V> implements Cache<K, V> {
  41. private Logger logger = LoggerFactory.getLogger(getClass());
  42. private String cacheKeyName = null;
  43. public JedisCache(String cacheKeyName) {
  44. this.cacheKeyName = cacheKeyName;
  45. // if (!JedisUtils.exists(cacheKeyName)){
  46. // Map<String, Object> map = Maps.newHashMap();
  47. // JedisUtils.setObjectMap(cacheKeyName, map, 60 * 60 * 24);
  48. // }
  49. // logger.debug("Init: cacheKeyName {} ", cacheKeyName);
  50. }
  51. @SuppressWarnings("unchecked")
  52. @Override
  53. public V get(K key) throws CacheException {
  54. if (key == null){
  55. return null;
  56. }
  57. V v = null;
  58. HttpServletRequest request = Servlets.getRequest();
  59. if (request != null){
  60. v = (V)request.getAttribute(cacheKeyName);
  61. if (v != null){
  62. return v;
  63. }
  64. }
  65. V value = null;
  66. Jedis jedis = null;
  67. try {
  68. jedis = JedisUtils.getResource();
  69. value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
  70. logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
  71. } catch (Exception e) {
  72. logger.error("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "", e);
  73. } finally {
  74. JedisUtils.returnResource(jedis);
  75. }
  76. if (request != null && value != null){
  77. request.setAttribute(cacheKeyName, value);
  78. }
  79. return value;
  80. }
  81. @Override
  82. public V put(K key, V value) throws CacheException {
  83. if (key == null){
  84. return null;
  85. }
  86. Jedis jedis = null;
  87. try {
  88. jedis = JedisUtils.getResource();
  89. jedis.hset(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key), JedisUtils.toBytes(value));
  90. logger.debug("put {} {} = {}", cacheKeyName, key, value);
  91. } catch (Exception e) {
  92. logger.error("put {} {}", cacheKeyName, key, e);
  93. } finally {
  94. JedisUtils.returnResource(jedis);
  95. }
  96. return value;
  97. }
  98. @SuppressWarnings("unchecked")
  99. @Override
  100. public V remove(K key) throws CacheException {
  101. V value = null;
  102. Jedis jedis = null;
  103. try {
  104. jedis = JedisUtils.getResource();
  105. value = (V)JedisUtils.toObject(jedis.hget(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key)));
  106. jedis.hdel(JedisUtils.getBytesKey(cacheKeyName), JedisUtils.getBytesKey(key));
  107. logger.debug("remove {} {}", cacheKeyName, key);
  108. } catch (Exception e) {
  109. logger.warn("remove {} {}", cacheKeyName, key, e);
  110. } finally {
  111. JedisUtils.returnResource(jedis);
  112. }
  113. return value;
  114. }
  115. @Override
  116. public void clear() throws CacheException {
  117. Jedis jedis = null;
  118. try {
  119. jedis = JedisUtils.getResource();
  120. jedis.hdel(JedisUtils.getBytesKey(cacheKeyName));
  121. logger.debug("clear {}", cacheKeyName);
  122. } catch (Exception e) {
  123. logger.error("clear {}", cacheKeyName, e);
  124. } finally {
  125. JedisUtils.returnResource(jedis);
  126. }
  127. }
  128. @Override
  129. public int size() {
  130. int size = 0;
  131. Jedis jedis = null;
  132. try {
  133. jedis = JedisUtils.getResource();
  134. size = jedis.hlen(JedisUtils.getBytesKey(cacheKeyName)).intValue();
  135. logger.debug("size {} {} ", cacheKeyName, size);
  136. return size;
  137. } catch (Exception e) {
  138. logger.error("clear {}", cacheKeyName, e);
  139. } finally {
  140. JedisUtils.returnResource(jedis);
  141. }
  142. return size;
  143. }
  144. @SuppressWarnings("unchecked")
  145. @Override
  146. public Set<K> keys() {
  147. Set<K> keys = Sets.newHashSet();
  148. Jedis jedis = null;
  149. try {
  150. jedis = JedisUtils.getResource();
  151. Set<byte[]> set = jedis.hkeys(JedisUtils.getBytesKey(cacheKeyName));
  152. for(byte[] key : set){
  153. keys.add((K)key);
  154. }
  155. logger.debug("keys {} {} ", cacheKeyName, keys);
  156. return keys;
  157. } catch (Exception e) {
  158. logger.error("keys {}", cacheKeyName, e);
  159. } finally {
  160. JedisUtils.returnResource(jedis);
  161. }
  162. return keys;
  163. }
  164. @SuppressWarnings("unchecked")
  165. @Override
  166. public Collection<V> values() {
  167. Collection<V> vals = Collections.emptyList();;
  168. Jedis jedis = null;
  169. try {
  170. jedis = JedisUtils.getResource();
  171. Collection<byte[]> col = jedis.hvals(JedisUtils.getBytesKey(cacheKeyName));
  172. for(byte[] val : col){
  173. vals.add((V)val);
  174. }
  175. logger.debug("values {} {} ", cacheKeyName, vals);
  176. return vals;
  177. } catch (Exception e) {
  178. logger.error("values {}", cacheKeyName, e);
  179. } finally {
  180. JedisUtils.returnResource(jedis);
  181. }
  182. return vals;
  183. }
  184. }
  185. }