package com.yiidata.intergration.web.modules.sys.cache; import com.google.common.collect.ImmutableSet; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.Collection; import java.util.HashSet; import java.util.Set; /** * * 支持缓存, @Cacheable 注解。 * * CAcheManager 无须自己 shutdown,创建 该 Cache 的 Factory 在销毁是会 shutdown * *
 *
 * User: zhenqin
 * Date: 14/11/9
 * Time: 10:43
 *
 * 
* * @author zhenqin */ @Component("cacheManager") public class ICacheManager implements CacheManager { /** * 支持 cacheManager 的缓存 */ @Resource private ICache cache; /** * 已经获得的缓存 */ private Set cacheNames = new HashSet<>(); /** * Construct a dynamic ICacheManager, * lazily creating cache instances as they are being requested. */ public ICacheManager() { } /** * 支持的 缓存 名称 * @return */ @Override public Collection getCacheNames() { return ImmutableSet.copyOf(cacheNames); } @Override public Cache getCache(String name) { if(!cacheNames.contains(name)) { // 如果 cacheNames 中没有,说明是新建的 cacheNames.add(name); } return new ProxyCache(cache); } public void setCache(ICache cache) { this.cache = cache; } }