首页>代码>spring+spring mvc+mybatis开发java web行业通用门户网站企业官网>/enterprise/src/main/java/com/enterprise/cache/EhcacheCacheProvider.java
package com.enterprise.cache; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import javax.annotation.PostConstruct; import org.springframework.core.io.Resource; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheException; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.config.Configuration; import net.sf.ehcache.config.ConfigurationFactory; public class EhcacheCacheProvider implements CacheProvider { private CacheManager cacheManager; private String cacheName; private Cache cache; private Resource configLocation; @Override public void put(String key, Serializable cacheObject) { cache.put(new Element(key, cacheObject)); } @Override public Serializable get(String key) { Element element = cache.get(key); return element != null ? element.getValue() : null; } @Override public void remove(String key) { cache.remove(key); } @Override public void clear() { cache.removeAll(); } public String getCacheName() { return cacheName; } public void setCacheName(String cacheName) { this.cacheName = cacheName; } public Resource getConfigLocation() { return configLocation; } public void setConfigLocation(Resource configLocation) { this.configLocation = configLocation; } @PostConstruct public void postCacheManager() throws IOException, CacheException { InputStream is = (this.configLocation != null ? this.configLocation.getInputStream() : null); try { Configuration configuration = (is != null ? ConfigurationFactory.parseConfiguration(is) : ConfigurationFactory.parseConfiguration()); this.cacheManager = cacheManager != null ? cacheManager : new CacheManager(configuration); } finally { if (is != null) { is.close(); } } if (cacheManager == null) { throw new CacheException("cache manage初始化失败"); } cache = cacheManager.getCache(cacheName); if (cache == null) { throw new CacheException("cache manager初始化失败"); } } }

F丶S丶H LV7
4月14日
alive13gyp LV2
3月19日
雨丝微凉 LV4
2024年10月16日
微信网友_7134912998903808 LV15
2024年9月2日
li739784276 LV6
2024年8月13日
hello_jugg LV7
2024年8月7日
TY0165 LV20
2024年6月24日
张德志 LV9
2024年6月17日
sccs1984 LV15
2024年4月24日
lilong007 LV23
2023年12月30日

luhong LV4
5月6日
F丶S丶H LV7
4月14日
alive13gyp LV2
3月19日
cassie555 LV3
1月21日
ma406805131 LV19
2024年12月18日
半夏bx LV14
2024年10月12日
微信网友_7134912998903808 LV15
2024年9月2日
li739784276 LV6
2024年8月13日
hello_jugg LV7
2024年8月7日
TY0165 LV20
2024年6月24日