1.查询的时候先查询缓存,如果为空就会以查询数据库,然后设置值到缓存
public List<ClmHospitalLccc> queryClmHospitalLccc() {
List<ClmHospitalLccc> list;
List<ClmHospitalLccc> lv01 = new ArrayList<>();
List<ClmHospitalLccc> lv02 = new ArrayList<>();
List<ClmHospitalLccc> lv03 = new ArrayList<>();
int count = 0;
RedisUtils redisUtils = RedisUtils.getInstance();
if(redisUtils.get(CustomerConstants.QUERY_XILL_HOSPITAL_TREE) !=null){
log.info("医院索引缓存数据:{}",(List<ClmHospitalLccc>) redisUtils.get(CustomerConstants.QUERY_XILL_HOSPITAL_TREE));
return (List<ClmHospitalLccc>) redisUtils.get(CustomerConstants.REGION_CUSTOMER,CustomerConstants.QUERY_XILL_HOSPITAL_TREE);
}
list = this.hospitalCodeUpholdMapper.initPageList();
for(ClmHospitalLccc clmHospitalLccc:list){
if(clmHospitalLccc.getHscode() != null && !"".equals(clmHospitalLccc.getHscode())){
if(clmHospitalLccc.getHscode().trim().length() == 2){
lv01.add(clmHospitalLccc);
}
if(clmHospitalLccc.getHscode().trim().length() == 4){
lv02.add(clmHospitalLccc);
}
if(clmHospitalLccc.getHscode().trim().length() == 6){
lv03.add(clmHospitalLccc);
}
}
}
for(ClmHospitalLccc clmHospitalLccc02:lv02){
List children = new ArrayList();
for(ClmHospitalLccc clmHospitalLccc03:lv03){
if(clmHospitalLccc03.getHscode().substring(0,4).equals(clmHospitalLccc02.getHscode())){
Map<String, Object> map = new HashMap();
map.put("hscode", clmHospitalLccc03.getHscode());
map.put("hsname", clmHospitalLccc03.getHsname());
map.put("children", clmHospitalLccc03.getChildren());
children.add(map);
}
}
if (children.size() > 0) {
clmHospitalLccc02.setChildren(children);
}
}
for(ClmHospitalLccc clmHospitalLccc01:lv01){
List children = new ArrayList();
for(ClmHospitalLccc clmHospitalLccc02:lv02){
if(clmHospitalLccc02.getHscode().substring(0,2).equals(clmHospitalLccc01.getHscode())){
Map<String, Object> map = new HashMap();
map.put("hscode", clmHospitalLccc02.getHscode());
map.put("hsname", clmHospitalLccc02.getHsname());
map.put("children", clmHospitalLccc02.getChildren());
children.add(map);
}
}
if (children.size() > 0) {
clmHospitalLccc01.setChildren(children);
}
}
log.info("医院索引查询-数据缓存处理前:{}",System.currentTimeMillis());
redisUtils.set(CustomerConstants.REGION_CUSTOMER, CustomerConstants.QUERY_XILL_HOSPITAL_TREE, lv01,30*24*3600);
log.info("医院索引查询-数据缓存处理后:{}",System.currentTimeMillis());
return lv01;
}
2.在进行修改(insert,delete,update)操作的时候进行 删除缓存(这样就会下次查询的时候缓存为空,又会重新设置缓存)
3.缓存工具类
package com.sunline.framework.redis.utils;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.util.StringUtils;
import com.sunline.framework.redis.constants.CttRedis.Region;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RedisUtils {
public static final String regionDefault = Region.REDIS_OBJ;
private static RedisUtils redisUtils;
private RedisTemplate<Serializable, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public static RedisUtils getInstance() {
if (null == redisUtils) {
synchronized (RedisUtils.class) {
if (null == redisUtils) {
log.info("缓存实例为空,初始化开始...");
redisUtils = new RedisUtils();
log.info("缓存实例为空,初始化完成...");
}
}
}
return redisUtils;
}
public Object get(String region, String key) {
try {
String factKey = this.getKeyName(region, key);
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
return operations.get(factKey);
} catch (Exception e) {
log.error("根据region[{}],key[{}]获取缓存时异常!", region, key);
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
return null;
}
public Object get(String key) {
return this.get(this.regionDefault, key);
}
public void set(String region, String key, Object value) {
try {
if (!StringUtils.isEmpty(key)) {
String factKey = this.getKeyName(region, key);
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(factKey, value);
}
} catch (Exception e) {
log.error("根据region[{}],key[{}]存储缓存时异常!", region, key);
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
public void set(String region, String key, Object value, int expireTime) {
try {
if (!StringUtils.isEmpty(key)) {
String factKey = this.getKeyName(region, key);
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(factKey, value);
redisTemplate.expire(factKey, expireTime, TimeUnit.SECONDS);
}
} catch (Exception e) {
log.error("根据region[{}],key[{}]存储缓存时异常!", region, key);
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
public void set(String key, Object value) {
this.set(regionDefault, key, value);
}
public void set(String key, Object value, int expireTime) {
try {
String factKey = this.getKeyName(regionDefault, key);
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(factKey, value);
redisTemplate.expire(factKey, expireTime, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("根据key[{}],expireTime[{}]存储缓存时异常!", key, expireTime);
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
public Set<Serializable> keys(String pattern) {
try {
return redisTemplate.keys(pattern);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
public void remove(String region, String key) {
try {
String factKey = this.getKeyName(region, key);
if (exists(factKey)) {
redisTemplate.delete(factKey);
}
} catch (Exception e) {
log.error("根据region[{}],key[{}]清理缓存时异常!", region, key);
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
public void remove(String key) {
try {
String factKey = this.getKeyName(this.regionDefault, key);
if (exists(factKey)) {
redisTemplate.delete(factKey);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
private boolean exists(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
return false;
}
public void clearAllStartingWith(String prefix) {
try {
Set<Serializable> setData = this.keys(prefix);
if (null != setData) {
redisTemplate.delete(setData);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
}
}
@SuppressWarnings("rawtypes")
protected String getKeyName(String region, Object key) {
if (StringUtils.isEmpty(region)) {
region = regionDefault;
}
if (key instanceof Number) {
return region + ":I:" + key;
} else {
Class keyClass = key.getClass();
if (String.class.equals(keyClass) || StringBuffer.class.equals(keyClass)
|| StringBuilder.class.equals(keyClass)) {
return region + ":S:" + key;
}
}
return region + ":O:" + key;
}
public long generate( ) {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(new Date().toString(), redisTemplate.getConnectionFactory());
return redisAtomicLong.incrementAndGet();
}
public long generate(String key) {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
return redisAtomicLong.incrementAndGet();
}
public long generate(String key,Date expireTime) {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
redisAtomicLong.expireAt(expireTime);
return redisAtomicLong.incrementAndGet();
}
public long generate(String key,int increment) {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
return redisAtomicLong.addAndGet(increment);
}
public long generate(String key,int increment,Date expireTime) {
RedisAtomicLong redisAtomicLong = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
redisAtomicLong.expireAt(expireTime);
return redisAtomicLong.addAndGet(increment);
}
public RedisTemplate<Serializable, Object> getRedisTemplate() {
return redisTemplate;
}
public Long setValueToSet(String region, String key, String value) {
String factKey = this.getKeyName(region, key);
return redisTemplate.opsForSet().add(factKey,value);
}
public Long removeFromSet(String region, String key, String value){
String factKey = this.getKeyName(region, key);
return redisTemplate.opsForSet().remove(factKey,value);
}
public Set<Object> getSet(String region, String key){
String factKey = this.getKeyName(region,key);
return redisTemplate.opsForSet().members(factKey);
}
}