java图片redis缓存_java本地缓存和redis缓存

本地缓存

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

首先需要引入包

net.sf.ehcache

ehcache

2.10.1

缓存服务接口:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值