Commit 8664c071 authored by huangcb's avatar huangcb

设置缓存前缀

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