diff --git a/pom.xml b/pom.xml index ee0f15b..a97df26 100644 --- a/pom.xml +++ b/pom.xml @@ -38,10 +38,10 @@ org.springframework.boot spring-boot-starter-aop - + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java new file mode 100644 index 0000000..6a0970b --- /dev/null +++ b/src/main/java/com/xkrs/common/config/RedisCacheAutoConfiguration.java @@ -0,0 +1,30 @@ +package com.xkrs.common.config; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.io.Serializable; + +/** + * Redis 缓存自动配置 + * @author tajochen + */ +@Configuration +@AutoConfigureAfter(RedisAutoConfiguration.class) +public class RedisCacheAutoConfiguration { + + @Bean + public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + template.setConnectionFactory(redisConnectionFactory); + return template; + } +} diff --git a/src/main/java/com/xkrs/common/config/RedisConfig.java b/src/main/java/com/xkrs/common/config/RedisConfig.java new file mode 100644 index 0000000..fb6641d --- /dev/null +++ b/src/main/java/com/xkrs/common/config/RedisConfig.java @@ -0,0 +1,187 @@ +package com.xkrs.common.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.interceptor.CacheErrorHandler; +import org.springframework.cache.interceptor.CacheResolver; +import org.springframework.cache.interceptor.KeyGenerator; +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.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.*; + +import javax.annotation.Resource; +import java.io.IOException; +import java.time.Duration; + +/** + * redis配置 + * @author tajochen + */ +@Configuration +@EnableCaching +public class RedisConfig extends CachingConfigurerSupport{ + + private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class); + + @Resource + private LettuceConnectionFactory lettuceConnectionFactory; + + private static final Duration TIME_TO_LIVE = Duration.ofSeconds(3600*6); + + @Bean + @Override + public KeyGenerator keyGenerator() { + + // 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存 + return (target, method, params) -> { + StringBuilder sb = new StringBuilder(); + sb.append(target.getClass().getName()); + sb.append(":"); + sb.append(method.getName()); + for (Object obj : params) { + sb.append(":").append(obj); + } + // logger.info("自动生成Redis Key -> [{}]", rsToUse); + return String.valueOf(sb); + }; + } + + @Bean + @Override + public CacheManager cacheManager() { + // 关键点,spring cache的注解使用的序列化都从这来,没有这个配置的话使用的jdk自己的序列化 + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() + //key序列化方式 + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer())) + //value序列化方式 + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer())) + .disableCachingNullValues() + .entryTtl(TIME_TO_LIVE); + + RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder + .fromConnectionFactory(lettuceConnectionFactory) + .cacheDefaults(config) + .transactionAware(); + + return builder.build(); + } + + + /** + * RedisTemplate配置 在单独使用redisTemplate的时候 重新定义序列化方式 + */ + @Bean(name = "redisTemplate") + public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { + // 设置序列化 + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + // 配置null值序列化成空字符串 + om.getSerializerProvider().setNullValueSerializer(new JsonSerializer<>() { + @Override + public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeString(""); + } + }); + // 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准 + JavaTimeModule javaTimeModule = new JavaTimeModule(); + om.registerModule(javaTimeModule); + om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + // 其他设置 + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , + ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + jackson2JsonRedisSerializer.setObjectMapper(om); + // 配置redisTemplate + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(lettuceConnectionFactory); + RedisSerializer stringSerializer = new StringRedisSerializer(); + redisTemplate.setKeySerializer(stringSerializer); + redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.setHashKeySerializer(stringSerializer); + redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + + private RedisSerializer keySerializer() { + return new StringRedisSerializer(); + } + + private RedisSerializer valueSerializer() { + // 设置序列化 + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>( + Object.class); + ObjectMapper om = new ObjectMapper(); + // 解决jackson无法反序列化LocalDateTime的问题,引入jsr310标准 + JavaTimeModule javaTimeModule = new JavaTimeModule(); + om.registerModule(javaTimeModule); + om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + // 其他设置 + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , + ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + jackson2JsonRedisSerializer.setObjectMapper(om); + return jackson2JsonRedisSerializer; + } + + @Override + public CacheResolver cacheResolver() { + return null; + } + + /** + * 设置数据存入 redis 的序列化方式,并开启事务 + * + * @param redisTemplate + * @param factory + */ + private void initRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) { + //如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String! + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); + redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + // 开启事务 + redisTemplate.setEnableTransactionSupport(true); + redisTemplate.setConnectionFactory(factory); + } + + /** + * 注入封装RedisTemplate 给RedisUtil提供操作类 + * @param redisTemplate + * @return RedisUtil + */ + @Bean(name = "redisUtil") + public RedisUtil redisUtil(RedisTemplate redisTemplate) { + RedisUtil redisUtil = new RedisUtil(); + redisUtil.setRedisTemplate(redisTemplate); + return redisUtil; + } + + @Override + @Bean + public CacheErrorHandler errorHandler() { + // 异常处理,当Redis发生异常时,打印日志,但是程序正常走 + logger.info("初始化 -> [{}]", "Redis CacheErrorHandler"); + return null; + } + +} diff --git a/src/main/java/com/xkrs/common/config/RedisUtil.java b/src/main/java/com/xkrs/common/config/RedisUtil.java new file mode 100644 index 0000000..25bd3a7 --- /dev/null +++ b/src/main/java/com/xkrs/common/config/RedisUtil.java @@ -0,0 +1,533 @@ +package com.xkrs.common.config; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.CollectionUtils; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * redis工具类 + * @author tajochen + */ +public class RedisUtil { + + private RedisTemplate redisTemplate; + + public void setRedisTemplate(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + + /** + * 指定缓存失效时间 + * @param key 键 + * @param time 时间(秒) + * @return + */ + public boolean expire(String key,long time){ + try { + if(time>0){ + redisTemplate.expire(key, time, TimeUnit.SECONDS); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 根据key 获取过期时间 + * @param key 键 不能为null + * @return 时间(秒) 返回0 代表为永久有效 + */ + public long getExpire(String key){ + return redisTemplate.getExpire(key,TimeUnit.SECONDS); + } + + /** + * 判断key是否存在 + * @param key 键 + * @return true 存在 false不存在 + */ + public boolean hasKey(String key){ + try { + return redisTemplate.hasKey(key); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 删除缓存 + * @param key 可以传一个值 或多个 + */ + @SuppressWarnings("unchecked") + public void del(String ... key){ + if(key!=null&&key.length>0){ + if(key.length==1){ + redisTemplate.delete(key[0]); + }else{ + redisTemplate.delete(String.valueOf(CollectionUtils.arrayToList(key))); + } + } + } + + //============================String============================= + /** + * 普通缓存获取 + * @param key 键 + * @return 值 + */ + public Object get(String key){ + return key==null?null:redisTemplate.opsForValue().get(key); + } + + /** + * 普通缓存放入 + * @param key 键 + * @param value 值 + * @return true成功 false失败 + */ + public boolean set(String key,Object value) { + try { + redisTemplate.opsForValue().set(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + /** + * 普通缓存放入并设置时间 + * @param key 键 + * @param value 值 + * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 + * @return true成功 false 失败 + */ + public boolean set(String key,Object value,long time){ + try { + if(time>0){ + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); + } else { + set(key, value); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 递增 + * @param key 键 + * @param delta 要增加几(大于0) + * @return + */ + public long incr(String key, long delta){ + if(delta<0){ + throw new RuntimeException("递增因子必须大于0"); + } + return redisTemplate.opsForValue().increment(key, delta); + } + + /** + * 递减 + * @param key 键 + * @param delta 要减少几(小于0) + * @return + */ + public long decr(String key, long delta){ + if(delta<0){ + throw new RuntimeException("递减因子必须大于0"); + } + return redisTemplate.opsForValue().increment(key, -delta); + } + + //================================Map================================= + /** + * HashGet + * @param key 键 不能为null + * @param item 项 不能为null + * @return 值 + */ + public Object hget(String key,String item){ + return redisTemplate.opsForHash().get(key, item); + } + + /** + * 获取hashKey对应的所有键值 + * @param key 键 + * @return 对应的多个键值 + */ + public Map hmget(String key){ + return redisTemplate.opsForHash().entries(key); + } + + /** + * HashSet + * @param key 键 + * @param map 对应多个键值 + * @return true 成功 false 失败 + */ + public boolean hmset(String key, Map map){ + try { + redisTemplate.opsForHash().putAll(key, map); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * HashSet 并设置时间 + * @param key 键 + * @param map 对应多个键值 + * @param time 时间(秒) + * @return true成功 false失败 + */ + public boolean hmset(String key, Map map, long time){ + try { + redisTemplate.opsForHash().putAll(key, map); + if(time>0){ + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 向一张hash表中放入数据,如果不存在将创建 + * @param key 键 + * @param item 项 + * @param value 值 + * @return true 成功 false失败 + */ + public boolean hset(String key,String item,Object value) { + try { + redisTemplate.opsForHash().put(key, item, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 向一张hash表中放入数据,如果不存在将创建 + * @param key 键 + * @param item 项 + * @param value 值 + * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 + * @return true 成功 false失败 + */ + public boolean hset(String key,String item,Object value,long time) { + try { + redisTemplate.opsForHash().put(key, item, value); + if(time>0){ + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 删除hash表中的值 + * @param key 键 不能为null + * @param item 项 可以使多个 不能为null + */ + public void hdel(String key, Object... item){ + redisTemplate.opsForHash().delete(key,item); + } + + /** + * 判断hash表中是否有该项的值 + * @param key 键 不能为null + * @param item 项 不能为null + * @return true 存在 false不存在 + */ + public boolean hHasKey(String key, String item){ + return redisTemplate.opsForHash().hasKey(key, item); + } + + /** + * hash递增 如果不存在,就会创建一个 并把新增后的值返回 + * @param key 键 + * @param item 项 + * @param by 要增加几(大于0) + * @return + */ + public double hincr(String key, String item,double by){ + return redisTemplate.opsForHash().increment(key, item, by); + } + + /** + * hash递减 + * @param key 键 + * @param item 项 + * @param by 要减少记(小于0) + * @return + */ + public double hdecr(String key, String item,double by){ + return redisTemplate.opsForHash().increment(key, item,-by); + } + + //============================set============================= + /** + * 根据key获取Set中的所有值 + * @param key 键 + * @return + */ + public Set sGet(String key){ + try { + return redisTemplate.opsForSet().members(key); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 根据value从一个set中查询,是否存在 + * @param key 键 + * @param value 值 + * @return true 存在 false不存在 + */ + public boolean sHasKey(String key,Object value){ + try { + return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, value)); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 将数据放入set缓存 + * @param key 键 + * @param values 值 可以是多个 + * @return 成功个数 + */ + public long sSet(String key, Object...values) { + try { + return redisTemplate.opsForSet().add(key, values); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + /** + * 将set数据放入缓存 + * @param key 键 + * @param time 时间(秒) + * @param values 值 可以是多个 + * @return 成功个数 + */ + public long sSetAndTime(String key,long time,Object...values) { + try { + Long count = redisTemplate.opsForSet().add(key, values); + if(time>0) + {expire(key, time);} + return count; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + /** + * 获取set缓存的长度 + * @param key 键 + * @return + */ + public long sGetSetSize(String key){ + try { + return redisTemplate.opsForSet().size(key); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + /** + * 移除值为value的 + * @param key 键 + * @param values 值 可以是多个 + * @return 移除的个数 + */ + public long setRemove(String key, Object ...values) { + try { + Long count = redisTemplate.opsForSet().remove(key, values); + return count; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + //===============================list================================= + + /** + * 获取list缓存的内容 + * @param key 键 + * @param start 开始 + * @param end 结束 0 到 -1代表所有值 + * @return + */ + public List lGet(String key, long start, long end){ + try { + return redisTemplate.opsForList().range(key, start, end); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 获取list缓存的长度 + * @param key 键 + * @return + */ + public long lGetListSize(String key){ + try { + return redisTemplate.opsForList().size(key); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + /** + * 通过索引 获取list中的值 + * @param key 键 + * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 + * @return + */ + public Object lGetIndex(String key,long index){ + try { + return redisTemplate.opsForList().index(key, index); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 将list放入缓存 + * @param key 键 + * @param value 值 + * @return + */ + public boolean lSet(String key, Object value) { + try { + redisTemplate.opsForList().rightPush(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 将list放入缓存 + * @param key 键 + * @param value 值 + * @param time 时间(秒) + * @return + */ + public boolean lSet(String key, Object value, long time) { + try { + redisTemplate.opsForList().rightPush(key, value); + if (time > 0) + {expire(key, time);} + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 将list放入缓存 + * @param key 键 + * @param value 值 + * @return + */ + public boolean lSet(String key, List value) { + try { + redisTemplate.opsForList().rightPushAll(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 将list放入缓存 + * @param key 键 + * @param value 值 + * @param time 时间(秒) + * @return + */ + public boolean lSet(String key, List value, long time) { + try { + redisTemplate.opsForList().rightPushAll(key, value); + if (time > 0) { + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 根据索引修改list中的某条数据 + * @param key 键 + * @param index 索引 + * @param value 值 + * @return + */ + public boolean lUpdateIndex(String key, long index,Object value) { + try { + redisTemplate.opsForList().set(key, index, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 移除N个值为value + * @param key 键 + * @param count 移除多少个 + * @param value 值 + * @return 移除的个数 + */ + public long lRemove(String key,long count,Object value) { + try { + Long remove = redisTemplate.opsForList().remove(key, count, value); + return remove; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } +} + diff --git a/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java b/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java new file mode 100644 index 0000000..9133374 --- /dev/null +++ b/src/main/java/com/xkrs/common/schedule/SaticScheduleTask.java @@ -0,0 +1,33 @@ +package com.xkrs.common.schedule; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; + +import java.time.LocalDateTime; + + +/** + * 定时任务 + * @author tajochen + */ +@Configuration +@EnableScheduling +public class SaticScheduleTask { + + Logger logger = LoggerFactory.getLogger(SaticScheduleTask.class); + + + /** + * 查询新的火点信息 + */ + @Scheduled(cron = "0 0/5 * * * ?") + private void configureTasks() { + logger.info("执行静态定时任务时间: " + LocalDateTime.now()); + System.out.println("------" + "开始执行定时任务"); +// tempQueryPoint.findPointToLocal(); + } + +} diff --git a/src/main/java/com/xkrs/controller/FireController.java b/src/main/java/com/xkrs/controller/FireController.java new file mode 100644 index 0000000..897e6a3 --- /dev/null +++ b/src/main/java/com/xkrs/controller/FireController.java @@ -0,0 +1,29 @@ +package com.xkrs.controller; + +import com.xkrs.service.FireService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @Author: XinYi Song + * @Date: 2022/2/11 9:03 + */ +@RestController +public class FireController { + + @Resource + private FireService fireService; + + /** + * 根据设备编号获取火情信息 + * @param code + * @return + */ + @GetMapping("/getFireInformation") + public String getFireInformation(@RequestParam("code") String code){ + return fireService.getFireInformation(code); + } +} diff --git a/src/main/java/com/xkrs/controller/TestController.java b/src/main/java/com/xkrs/controller/TestController.java index 477c5fa..699e675 100644 --- a/src/main/java/com/xkrs/controller/TestController.java +++ b/src/main/java/com/xkrs/controller/TestController.java @@ -114,6 +114,7 @@ public class TestController { JsonNode jsonNode = results.get(0); JsonNode daily = jsonNode.path("daily"); JsonNode jsonNode1 = daily.get(0); + return jsonNode1; } } diff --git a/src/main/java/com/xkrs/dao/FireDao.java b/src/main/java/com/xkrs/dao/FireDao.java index 53026cf..439f5a9 100644 --- a/src/main/java/com/xkrs/dao/FireDao.java +++ b/src/main/java/com/xkrs/dao/FireDao.java @@ -6,6 +6,8 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Component; +import java.util.List; + /** * @Author: XinYi Song * @Date: 2022/2/8 17:12 @@ -28,4 +30,11 @@ public interface FireDao extends JpaRepository { @Modifying(clearAutomatically=true) @Query(value = "update fire set picture_path = ?2 where alarm_code = ?1",nativeQuery = true) void updatePicturePath(String code, String picturePath); + + /** + * 根据设备编码获取火情的信息 + * @param code + * @return + */ + List findByDeviceCode(String code); } diff --git a/src/main/java/com/xkrs/service/FireService.java b/src/main/java/com/xkrs/service/FireService.java new file mode 100644 index 0000000..968695a --- /dev/null +++ b/src/main/java/com/xkrs/service/FireService.java @@ -0,0 +1,15 @@ +package com.xkrs.service; + +/** + * @Author: XinYi Song + * @Date: 2022/2/11 8:51 + */ +public interface FireService { + + /** + * 根据设备编码获取火情信息 + * @param code + * @return + */ + String getFireInformation(String code); +} diff --git a/src/main/java/com/xkrs/service/impl/FireServerImpl.java b/src/main/java/com/xkrs/service/impl/FireServerImpl.java new file mode 100644 index 0000000..3ed3e7d --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/FireServerImpl.java @@ -0,0 +1,46 @@ +package com.xkrs.service.impl; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.FireDao; +import com.xkrs.model.entity.Fire; +import com.xkrs.service.FireService; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * @Author: XinYi Song + * @Date: 2022/2/11 8:52 + */ +@Service +@CacheConfig(cacheNames = "FireServiceCache") +public class FireServerImpl implements FireService { + + @Resource + private FireDao fireDao; + + Locale locale = LocaleContextHolder.getLocale(); + + + /** + * 根据设备编码获取火情的信息 + * @param code + * @return + */ + @Cacheable(keyGenerator = "keyGenerator",unless="#result == null") + @Override + public String getFireInformation(String code) { + List byDeviceCode = fireDao.findByDeviceCode(code); + if(byDeviceCode == null || byDeviceCode.size() == 0){ + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"暂时没有该设备的火情信息!",locale); + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,byDeviceCode,locale); + } +} diff --git a/src/main/java/com/xkrs/util/TopicConsumerListener.java b/src/main/java/com/xkrs/util/TopicConsumerListener.java index a8bbcd7..2f42976 100644 --- a/src/main/java/com/xkrs/util/TopicConsumerListener.java +++ b/src/main/java/com/xkrs/util/TopicConsumerListener.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.xkrs.dao.FireDao; import com.xkrs.model.entity.Fire; import com.xkrs.websocket.server.WebSocketServer; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @@ -31,6 +32,7 @@ public class TopicConsumerListener { * 订阅火情报警信息 * @param message */ + @CacheEvict(value = "FireServiceCache",allEntries = true) @JmsListener(destination="${spring.activemq.topic-name}", containerFactory="topicListener") public void readActiveQueue(String message) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 547c22e..4c725ae 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -32,17 +32,17 @@ spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false ## Redis配置 12 -#spring.cache.type = redis -#spring.redis.database = 12 -#spring.redis.host = localhost -#spring.redis.port = 6379 -#spring.redis.password=sdust2020 -#spring.redis.timeout = 10000 -#spring.redis.lettuce.pool.max-active = 100 -#spring.redis.lettuce.pool.max-wait = 10000 -#spring.redis.lettuce.pool.max-idle = 100 -#spring.redis.lettuce.pool.min-idle = 1 -#spring.redis.lettuce.shutdown-timeout = 0 5A8B67D1-3A8D-A942-8DC4-5D463BA900B3 506E8FC6-11DA-FE46-8DE4-38CCD353EA20 +spring.cache.type = redis +spring.redis.database = 14 +spring.redis.host = localhost +spring.redis.port = 6379 +spring.redis.password=sdust2020 +spring.redis.timeout = 10000 +spring.redis.lettuce.pool.max-active = 100 +spring.redis.lettuce.pool.max-wait = 10000 +spring.redis.lettuce.pool.max-idle = 100 +spring.redis.lettuce.pool.min-idle = 1 +spring.redis.lettuce.shutdown-timeout = 0 spring.activemq.broker-url=tcp://111.26.161.203:61616 spring.activemq.packages.trust-all=true