气象预警,历史气象预警新增缓存
parent
0060bd7b1a
commit
1329a6e096
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.whdc.config;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.data.redis.cache.RedisCache;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
* Created by XuSan on 2024/6/3.
|
||||||
|
*
|
||||||
|
* @author XuSan
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class CustomRedisCacheManager extends RedisCacheManager {
|
||||||
|
|
||||||
|
|
||||||
|
public CustomRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
|
||||||
|
super(cacheWriter, defaultCacheConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对@Cacheable设置缓存过期时间
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* @param cacheConfig
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) {
|
||||||
|
String[] array = StringUtils.delimitedListToStringArray(name, "#");
|
||||||
|
name = array[0];
|
||||||
|
// 解析TTL
|
||||||
|
if (array.length > 1) {
|
||||||
|
long ttl = Long.parseLong(array[1]);
|
||||||
|
cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(ttl)); // 注意单位我此处用的是秒,而非毫秒
|
||||||
|
}
|
||||||
|
return super.createRedisCache(name, cacheConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,111 @@
|
||||||
|
package com.whdc.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
* Created by XuSan on 2024/6/3.
|
||||||
|
*
|
||||||
|
* @author XuSan
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||||
|
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableCaching
|
||||||
|
public class RedisConfig implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 申明缓存管理器,会创建一个切面(aspect)并触发Spring缓存注解的切点(pointcut)
|
||||||
|
* 根据类或者方法所使用的注解以及缓存的状态,这个切面会从缓存中获取数据,将数据添加到缓存之中或者从缓存中移除某个值
|
||||||
|
*/
|
||||||
|
/* @Bean
|
||||||
|
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
return RedisCacheManager.create(redisConnectionFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
|
||||||
|
// 创建一个模板类
|
||||||
|
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
||||||
|
// 将刚才的redis连接工厂设置到模板类中
|
||||||
|
template.setConnectionFactory(factory);
|
||||||
|
// 设置key的序列化器
|
||||||
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
|
// 设置value的序列化器
|
||||||
|
//使用Jackson 2,将对象序列化为JSON
|
||||||
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||||
|
//json转对象类,不设置默认的会将json转成hashmap
|
||||||
|
ObjectMapper om = new ObjectMapper();
|
||||||
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||||
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||||
|
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||||
|
|
||||||
|
return template;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最新版,设置redis缓存过期时间
|
||||||
|
*/
|
||||||
|
// @Bean
|
||||||
|
// public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
// RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
|
||||||
|
// .entryTtl(Duration.ofDays(1))
|
||||||
|
// .computePrefixWith(cacheName -> "caching:" + cacheName);
|
||||||
|
//
|
||||||
|
// return new CustomRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), defaultCacheConfig);
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
return new RedisCacheManager(
|
||||||
|
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
|
||||||
|
this.getRedisCacheConfigurationWithTtl( -1), // 默认策略,未配置的 key 会使用这个
|
||||||
|
this.getRedisCacheConfigurationMap() // 指定 key 策略
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
|
||||||
|
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
|
||||||
|
|
||||||
|
//自定义设置缓存时间
|
||||||
|
redisCacheConfigurationMap.put("fxkh:txl:warning", this.getRedisCacheConfigurationWithTtl(120));
|
||||||
|
|
||||||
|
return redisCacheConfigurationMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
|
||||||
|
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||||
|
ObjectMapper om = new ObjectMapper();
|
||||||
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||||
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||||
|
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
|
||||||
|
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
|
||||||
|
RedisSerializationContext
|
||||||
|
.SerializationPair
|
||||||
|
.fromSerializer(jackson2JsonRedisSerializer)
|
||||||
|
).entryTtl(Duration.ofSeconds(seconds));
|
||||||
|
|
||||||
|
return redisCacheConfiguration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -53,7 +53,7 @@ import static com.whdc.model.MyConstant.REDIS_KEY;
|
||||||
@Transactional
|
@Transactional
|
||||||
public class AddressBookController {
|
public class AddressBookController {
|
||||||
|
|
||||||
public static final String ADDRESS_BOOK_REDIS_KEY = REDIS_KEY + "addressbook:";
|
public static final String ADDRESS_BOOK_REDIS_KEY = REDIS_KEY + "addressbook";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IAddressBookService service;
|
private IAddressBookService service;
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,8 @@ public class UserController {
|
||||||
private IUserService service;
|
private IUserService service;
|
||||||
|
|
||||||
|
|
||||||
// @ApiOperation(value = "登录", notes = "登录后,从 tokenInfo 中获取 token 相关信息。headers[tokenName] = tokenValue ")
|
@ApiOperation(value = "登录", notes = "登录后,从 tokenInfo 中获取 token 相关信息。headers[tokenName] = tokenValue ")
|
||||||
// @PostMapping("doLogin")
|
@PostMapping("doLogin")
|
||||||
public ResultJson<LoginVo> doLogin(@RequestBody @Validated LoginDto obj) throws InvocationTargetException, IllegalAccessException {
|
public ResultJson<LoginVo> doLogin(@RequestBody @Validated LoginDto obj) throws InvocationTargetException, IllegalAccessException {
|
||||||
LoginVo loginVo = service.login(obj);
|
LoginVo loginVo = service.login(obj);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import org.apache.commons.compress.utils.Lists;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
@ -30,6 +31,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.whdc.model.MyConstant.REDIS_KEY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description:
|
* Description:
|
||||||
* Created by XuSan on 2024/5/23.
|
* Created by XuSan on 2024/5/23.
|
||||||
|
|
@ -43,6 +46,8 @@ import java.util.stream.Collectors;
|
||||||
@RequestMapping("/warning")
|
@RequestMapping("/warning")
|
||||||
public class WarningController {
|
public class WarningController {
|
||||||
|
|
||||||
|
public static final String THIS_REDIS_KEY = REDIS_KEY + "warning";
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IAddressBookService addressBookService;
|
private IAddressBookService addressBookService;
|
||||||
|
|
@ -58,6 +63,7 @@ public class WarningController {
|
||||||
*/
|
*/
|
||||||
@ApiOperation(value = "气象预警")
|
@ApiOperation(value = "气象预警")
|
||||||
@PostMapping("/getGroupWarning")
|
@PostMapping("/getGroupWarning")
|
||||||
|
@Cacheable(value = THIS_REDIS_KEY, key = "#root.method.name+':'+#dto.toString()")
|
||||||
public ResultJson<List<WarningListVo>> getGroupWarning(
|
public ResultJson<List<WarningListVo>> getGroupWarning(
|
||||||
@RequestBody GroupWarningDto dto) {
|
@RequestBody GroupWarningDto dto) {
|
||||||
|
|
||||||
|
|
@ -135,6 +141,7 @@ public class WarningController {
|
||||||
*/
|
*/
|
||||||
@ApiOperation(value = "历史气象预警统计")
|
@ApiOperation(value = "历史气象预警统计")
|
||||||
@PostMapping("/getHistoryGroupWarning")
|
@PostMapping("/getHistoryGroupWarning")
|
||||||
|
@Cacheable(value = THIS_REDIS_KEY, key = "#root.method.name+':'+#dto.toString()")
|
||||||
public ResultJson<List<WarningHistoryListVo>> getHistoryGroupWarning(@RequestBody GroupWarningDto dto) {
|
public ResultJson<List<WarningHistoryListVo>> getHistoryGroupWarning(@RequestBody GroupWarningDto dto) {
|
||||||
|
|
||||||
ApiDto apiDto = new ApiDto();
|
ApiDto apiDto = new ApiDto();
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ import static com.whdc.model.MyConstant.REDIS_KEY;
|
||||||
@Service
|
@Service
|
||||||
public class AdinfoServiceImpl extends ServiceImpl<AdinfoMapper, Adinfo> implements IAdinfoService {
|
public class AdinfoServiceImpl extends ServiceImpl<AdinfoMapper, Adinfo> implements IAdinfoService {
|
||||||
|
|
||||||
public static final String ADINFO_REDIS_KEY = REDIS_KEY + "adinfo:";
|
public static final String ADINFO_REDIS_KEY = REDIS_KEY + "adinfo";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IPage<Adinfo> page(Adinfo dto) {
|
public IPage<Adinfo> page(Adinfo dto) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue