参考: 一级缓存二级缓存的获取与更新顺序(一)
简单封装Ehcache与RedisTemplate模版
通常使用一二级缓存时,必须保持一二级缓存数据数据与数据库中数据保持一致 ;此时可以简单封装下,一二级缓存的相关接口,便于我们同步数据操作;
Ehcache封装
package org.jd.auth.data.security.server.base;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.Map;
/**
* ehcache缓存服务组件:
* 这样重写Ehcache缓存组件的目的在于使用一级缓存和二级缓存时,便于及时同步一二级缓存的数据
* @Author: yh19166
*/
@Slf4j
@Component
public class SSOEhcacheService {
@Resource
@Qualifier("ehCacheManager")
private CacheManager cacheManager;
/**
* @param cacheName 为ehcache.xml文件中配置缓存的名称
* @return
*/
public Cache getCache(String cacheName) {
Cache cache = this.cacheManager.getCache(cacheName, String.class, Object.class);
if (ObjectUtils.isEmpty(cache)) { // 避免了Optional对象为Optional.empty()空对象;ObjectUtils.isEmpty(方法源代码已经作了判断)
log.error("SSOEhcacheService.getCache.cacheName[{}] is not exit", cacheName);
}
return cache;
}
/**
* 根据cacheKey获取对应的值
*
* @param cacheName 配置文件中配置缓存的名称
* @param cacheKey 缓存Key
* @return {@link Object}
*/
public Object get(String cacheName, String cacheKey) {
Cache cache = getCache(cacheName);
if (ObjectUtils.isEmpty(cache)) {
log.error("SSOEhcacheService.do.get method,[{}] is not exit", cacheName);
return null;
}
return cache.get(cacheKey);
}
/**
* @param cacheName 配置文件中配置缓存的名称
* @param cacheKey 缓存key
* @param value 缓存值
*/
public void put(String cacheName, String cacheKey, Object value) {
Cache cache = getCache(cacheName);
if (ObjectUtils.isEmpty(cache)) {
log.error("SSOEhcacheService.do.put method,[{}] is not exit", cacheName);
return;
}
cache.put(cacheKey, value);
}
/**
* @param cacheName 配置文件中配置缓存的名称
* @param value 缓存值
*/
public void putAll(String cacheName, Map value) {
Cache cache = getCache(cacheName);
if (ObjectUtils.isEmpty(cache)) {
log.error("SSOEhcacheService.do.putAll method,[{}] is not exit", cacheName);
return;
}
cache.putAll(value);
}
/**
* 判断缓存key是否存在
*
* @param cacheName 配置文件中配置缓存的名称
* @param key 缓存key
* @return {@link Boolean}
*/
public boolean exist(String cacheName, String key) {
Cache cache = getCache(cacheName);
if (ObjectUtils.isEmpty(cache)) {
log.error("SSOEhcacheService.do.exist method,[{}] is not exit", cacheName);
return false;
}
return cache.containsKey(key);
}
/**
* 删除指定key的缓存
*
* @param cacheName 配置文件中配置缓存的名称
* @param key 缓存key
*/
public void evict(String cacheName, String key) {
Cache cache = getCache(cacheName);
if (ObjectUtils.isEmpty(cache)) {
log.error("SSOEhcacheService.do.evict method,[{}] is not exit", cacheName);
return;
}
cache.remove(key);
}
}
RedisTemplate模版简单封装
package org.jd.auth.data.security.server.base;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* Redis通常作为二级缓存
* Redis缓存对: spring-boot-starter-data-redis的模版(RedisTemplate)的进一步封装:
* @Author: yh19166
*/
@Slf4j
@Component
public class SSORedisService {
@Resource
private RedisTemplate redisTemplate;
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 正则批量删除key 例:h?llo matches hello, hallo
*
* @param pattern
*/
public void removePattern(String pattern) {
Set keys = redisTemplate.keys(pattern);
if (!keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
/**
* 正则获取多个缓存key 例:h?llo matches hello, hallo
*
* @param pattern
*/
public Set patternKeys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 正则key获取多个缓存
*
* @param pattern
* @return 返回Set >-String 集合
*/
public Set patternKeysIsStr(String pattern) {
Set keys = redisTemplate.keys(pattern);
Set dataSet = new HashSet<>();
for (Serializable obj : keys) {
dataSet.add(obj.toString());
}
return dataSet;
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
/**
* 设置key的过期时间 单位(秒)
*
* @param key
* @param expireTime
* @return boolean
*/
public boolean setKeyExpire(String key, Long expireTime) {
return redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public Object getObject(String key) {
Object result = null;
ValueOperations operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean setObject(String key, Object value) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
log.warn("setObject 返回异常 " + e.getMessage());
}
return result;
}
/**
* 写入缓存设置并设置时间 时间单位(秒)
*
* @param key
* @param value
* @return
*/
public boolean setObjectAndExpire(String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
log.warn("setObjectAndExpire 返回异常 " + e.getMessage());
}
return result;
}
/**
* 缓存List数据 右侧入栈
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public boolean setList(String key, List