java本地緩存和redis緩存
阿新 • • 發佈:2018-03-19
fig etc erro obj 本地 pack manager bsp factor
本地緩存
本地緩存存儲在內存當中,實現緩存如下
首先需要引入包
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.1</version> </dependency>
緩存服務接口:
package com.mobcb.platform.service.common; import net.sf.ehcache.Cache; public interface EhcacheService { public void clearCache(String cacheName, String cacheKey); public void putCache(String cacheName, String cacheKey, Object value); public Object getCacheValue(String cacheName, String cacheKey); public Cache getCache(String cacheName); /** * 設置緩存 * * @param cacheName 緩存名稱 * @param cacheKey 緩存key * @param value 值 * @param timeToLiveSeconds 存在時間,單位秒 */ public void putCache(String cacheName, String cacheKey, Object value, int timeToLiveSeconds); }
EhcacheService 接口實現:
package com.mobcb.platform.service.impl.common; import com.mobcb.platform.service.common.EhcacheService; import javax.annotation.Resource; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Service; @Service("ehcacheService") public class EhcacheServiceImpl implements EhcacheService { private static Log logger = LogFactory.getLog(EhcacheServiceImpl.class); @Resource(name = "ehCacheManager") private CacheManager cacheManger; private boolean createCacheIfNotFound = true; @Override public void clearCache(String cacheName, String cacheKey) { Cache cache = getCache(cacheName); if (cache == null) { return; } cache.remove(cacheKey); } @Override public void putCache(String cacheName, String cacheKey, Object value) { Cache cache = getCache(cacheName); if (cache == null) { return; } cache.put(new Element(cacheKey, value)); } @Override public Object getCacheValue(String cacheName, String cacheKey) { Cache cache = getCache(cacheName); if (cache == null) { return null; } Element element = cache.get(cacheKey); if (element == null || element.isExpired()) { return null; } return element.getObjectValue(); } @Override public Cache getCache(String cacheName) { Cache cache = cacheManger.getCache(cacheName); if (cache == null && createCacheIfNotFound) { cache = (Cache) cacheManger.addCacheIfAbsent(cacheName); } if (cache == null) { logger.error("EHCache: cache not config and not auto created, cacheName=" + cacheName); } return cache; } @Override public void putCache(String cacheName, String cacheKey, Object value, int timeToLiveSeconds) { Cache cache = getCache(cacheName); if (cache == null) { return; } cache.put(new Element(cacheKey, value, timeToLiveSeconds, timeToLiveSeconds)); } }
緩存使用示例:
引入緩存服務接口
/** * 緩存服務 */ @Resource(name = "ehcacheService") private EhcacheService ehcacheService;
調用:
Object requestSource = ehcacheService.getCacheValue( "SHCAuth", "SHCAuthKey"); logger.info("------------------------緩存獲取認證碼"+requestSource+"-----------------------");
第一個參數是緩存名,第二個是緩存名下的鍵值
Redis緩存
java本地緩存和redis緩存