ICacheManager.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.yiidata.intergration.web.modules.sys.cache;
  2. import com.google.common.collect.ImmutableSet;
  3. import org.springframework.cache.Cache;
  4. import org.springframework.cache.CacheManager;
  5. import org.springframework.stereotype.Component;
  6. import javax.annotation.Resource;
  7. import java.util.Collection;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10. /**
  11. *
  12. * 支持缓存, @Cacheable 注解。
  13. *
  14. * CAcheManager 无须自己 shutdown,创建 该 Cache 的 Factory 在销毁是会 shutdown
  15. *
  16. * <pre>
  17. *
  18. * User: zhenqin
  19. * Date: 14/11/9
  20. * Time: 10:43
  21. *
  22. * </pre>
  23. *
  24. * @author zhenqin
  25. */
  26. @Component("cacheManager")
  27. public class ICacheManager implements CacheManager {
  28. /**
  29. * 支持 cacheManager 的缓存
  30. */
  31. @Resource
  32. private ICache cache;
  33. /**
  34. * 已经获得的缓存
  35. */
  36. private Set<String> cacheNames = new HashSet<>();
  37. /**
  38. * Construct a dynamic ICacheManager,
  39. * lazily creating cache instances as they are being requested.
  40. */
  41. public ICacheManager() {
  42. }
  43. /**
  44. * 支持的 缓存 名称
  45. * @return
  46. */
  47. @Override
  48. public Collection<String> getCacheNames() {
  49. return ImmutableSet.copyOf(cacheNames);
  50. }
  51. @Override
  52. public Cache getCache(String name) {
  53. if(!cacheNames.contains(name)) {
  54. // 如果 cacheNames 中没有,说明是新建的
  55. cacheNames.add(name);
  56. }
  57. return new ProxyCache(cache);
  58. }
  59. public void setCache(ICache cache) {
  60. this.cache = cache;
  61. }
  62. }