Commit 8664c071 authored by huangcb's avatar huangcb

设置缓存前缀

parent a375fce5
......@@ -3,10 +3,12 @@ package com.esv.datacenter.base.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.CacheKeyPrefix;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
......@@ -31,6 +33,9 @@ import java.time.Duration;
@EnableCaching
public class CacheConfig {
@Value("${spring.application.name}")
private String applicationName;
/**
* description 为SpringCache注册缓存管理器
* param [redisConnectionFactory]
......@@ -52,6 +57,7 @@ public class CacheConfig {
// 配置序列化(解决乱码的问题)
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.computePrefixWith(cacheKeyPrefix())
.entryTtl(Duration.ofMinutes(timeToLive))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
......@@ -62,6 +68,22 @@ public class CacheConfig {
return cacheManager;
}
/**
* @description 设置缓存key的前缀
* @return org.springframework.data.redis.cache.CacheKeyPrefix
* @author huangChaobin@esvtek.com
* @createTime 2020/07/30 19:49
**/
@Bean
public CacheKeyPrefix cacheKeyPrefix() {
return cacheName -> {
StringBuilder sBuilder = new StringBuilder(8);
sBuilder.append(applicationName).append("::");
sBuilder.append(cacheName).append("::");
return sBuilder.toString();
};
}
/**
* RedisTemplate相关配置
* @param factory
......
......@@ -20,7 +20,7 @@ import java.util.Map;
public class BaseCityCodeServiceImpl extends ServiceImpl<BaseCityCodeDao, BaseCityCodeEntity> implements BaseCityCodeService {
@Override
@Cacheable(value = "htwl-base-service::geo::city", key="'allTree'")
@Cacheable(value = "geo::city", key="'allTree'")
public List<JSONObject> getAllRegionTree() {
// 查询全国行政区划列表
List<BaseCityCodeEntity> cityCodeEntityList = this.baseMapper.selectList(new LambdaQueryWrapper<BaseCityCodeEntity>()
......@@ -85,7 +85,7 @@ public class BaseCityCodeServiceImpl extends ServiceImpl<BaseCityCodeDao, BaseCi
}
@Override
@Cacheable(value = "htwl-base-service::geo::city", key="'allMap'")
@Cacheable(value = "geo::city", key="'allMap'")
public Map<String, String> getAllRegionMap() {
// 查询全国行政区划列表
List<BaseCityCodeEntity> cityCodeEntityList = this.baseMapper.selectList(new LambdaQueryWrapper<BaseCityCodeEntity>()
......
......@@ -30,11 +30,10 @@ public class DicTypeServiceImpl implements DicTypeService {
}
@Override
@Cacheable(value = "htwl-base-service::dict::type", key="#type")
@Cacheable(value = "dict::type", key="#type")
public List<DicEntity> getDictByType(String type) {
QueryWrapper<DicEntity> queryWrapper = new QueryWrapper<>();
queryWrapper
.select("code", "name")
queryWrapper.select("code", "name")
.eq("type", type)
.orderByAsc("order_num");
......@@ -42,7 +41,7 @@ public class DicTypeServiceImpl implements DicTypeService {
}
@Override
@Cacheable(value = "htwl-base-service::dict::type", key="'allValid'")
@Cacheable(value = "dict::type", key="'allValid'")
public List<String> getAllValidType() {
return dicDao.selectAllValidType();
}
......
......@@ -22,7 +22,7 @@ import java.util.Map;
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeDao, GoodsTypeEntity> implements GoodsTypeService {
@Override
@Cacheable(value = "htwl-base-service::goods-type", key="'allValid'")
@Cacheable(value = "goods-type", key="'allValid'")
public List<TreeVO> getAllGoodsType() {
// 查询
QueryWrapper<GoodsTypeEntity> queryWrapper = new QueryWrapper<>();
......@@ -66,7 +66,7 @@ public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeDao, GoodsTypeEnt
}
@Override
@Cacheable(value = "htwl-base-service::goods-type", key="#code")
@Cacheable(value = "goods-type", key="#code")
public String getGoodsNameByCode(Integer code) {
GoodsTypeEntity entity = new GoodsTypeEntity();
entity.setCode(code);
......
......@@ -22,7 +22,7 @@ import java.util.Map;
public class VehicleTypeServiceImpl extends ServiceImpl<VehicleTypeDao, VehicleTypeEntity> implements VehicleTypeService {
@Override
@Cacheable(value = "htwl-base-service::vehicle-type", key="'allValid'")
@Cacheable(value = "vehicle-type", key="'allValid'")
public List<TreeVO> getAllVehicleType() {
// 查询
QueryWrapper<VehicleTypeEntity> queryWrapper = new QueryWrapper<>();
......@@ -66,7 +66,7 @@ public class VehicleTypeServiceImpl extends ServiceImpl<VehicleTypeDao, VehicleT
}
@Override
@Cacheable(value = "htwl-base-service::vehicle-type", key="#code")
@Cacheable(value = "vehicle-type", key="#code")
public String getVehicleNameByCode(Integer code) {
VehicleTypeEntity entity = new VehicleTypeEntity();
entity.setCode(code);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment