java本地缓存和redis缓存

                                                                                              本地缓存

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

首先需要引入包

 

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

缓存接口:

 

package com.mobcb.platform.service.common;

/**

  • 缓存服务,提供缓存操作方法

  • <p>

  • </p>

  • Created by storm on 2016-05-26.
    */
    public interface CacheService {

    /**

    • 清除缓存
    • @param cacheName 缓存名称
    • @param cacheKey 缓存 key
      */
      void clearCache(String cacheName, String cacheKey);

    /**

    • 增加缓存
    • @param cacheName 缓存名称
    • @param cacheKey 缓存 key
    • @param value 值
      */
      void putCache(String cacheName, String cacheKey, Object value);

    /**

    • 获取缓存值
    • @param cacheName 缓存名称
    • @param cacheKey 缓存 key
    • @return
      */
      Object getCacheValue(String cacheName, String cacheKey);

}

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------
缓存接口实现:
                         
package com.mobcb.platform.service.impl.common;

import com.mobcb.platform.service.common.CacheService;
import javax.annotation.Resource;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * Created by storm on 2016-05-26.
 */
@Service("cacheService")
public class CacheServiceImpl implements CacheService {@Resource(name = "cacheManager")
    private CacheManager cacheManger;

    @Override
    public void clearCache(String cacheName, String cacheKey) {
        // 清除缓存
        Cache cache = getCache(cacheName);
        if(cache == null) {return;}
        cache.evict(cacheName + "-" + cacheKey);
    }

    @Override
    public void putCache(String cacheName, String cacheKey, Object value) {Cache cache = getCache(cacheName);
        if(cache == null) {return;}
        cache.put(cacheName + "-" + cacheKey, value);
    }

    @Override
    public Object getCacheValue(String cacheName, String cacheKey) {Cache cache = getCache(cacheName);
        if(cache == null) {return null;}
        Cache.ValueWrapper element = cache.get(cacheName + "-" + cacheKey);
        if(element == null) {return null;}
        return element.get();}

    public Cache getCache(String cacheName) {Cache cache = cacheManger.getCache(cacheName);
        return cache;
    }
}
  ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

cacheManger 配置如下:

 

@Bean
public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
// cacheManager.setDefaultExpiration(86400);
cacheManager.setDefaultExpiration(172800);
return cacheManager;

}