码迷,mamicode.com
首页 > 编程语言 > 详细

java本地缓存和redis缓存

时间:2018-03-19 10:56:55      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:error   mob   get   live   etc   缓存   package   ice   nts   

  本地缓存

本地缓存存储在内存当中,实现缓存如下

首先需要引入包

 

<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缓存

标签:error   mob   get   live   etc   缓存   package   ice   nts   

原文地址:https://www.cnblogs.com/tiancaizhu/p/8600414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!