SpringBoot使用redis缓存List
一、概述
最近在做性能优化,之前有一个业务是这样实现的:
1.温度报警后第三方通讯管理机直接把报警信息保存到数据库
2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务逻辑,在数据库中插入“其他业务数据”
3.前端setTimeout每隔5秒ajax去后端查询“其他业务数据”(查库)
优化后这样实现:
两个微服务,消息中间件专门一个服务,接收消息存入数据库,存入redis;业务服务直接从redis获取
1.MQTT订阅通讯管理机报警事件主题
2.发生报警后,java中根据报警信息保存“其他业务数据”到数据库并放入redis缓存
3.前端setTimeout每隔5秒ajax去后端查询“其他业务数据”(改为从redis中获取)
4.下一步计划使用WebSocekt,去掉前端setTimeout
二、SpringBoot配置redis
pom.xml、application.properties、@EnableCaching等等这些配置就不列出来了,大家可以百度,提一下RedisTemplate的配置
RedisTemplate
package ; import java.lang.reflect.Method; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.CacheManager;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.JsonInclude.Include;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.ObjectMapper; @Configurationpublic class RedisConfiguration { @Bean("jsonRedisCache") public CacheManager cacheManager(@Autowired RedisTemplate
@Bean public RedisTemplate
三、List对象存入redis遇到的问题
1.@Cacheable不起作用问题
刚开始,计划在service层方法上使用注解@Cacheable进行缓存,但是redis没有保存,最后百度得到答案:一个类中@Cacheable标注的方法不能被本类中其他方法调用,否则缓存不起作用
修改类方法调用后此问题解决
错误的方法调用:
正确的调用:其他类调用该方法
这其中有个报错:No cache could be resolved for 'Builder[public java.util.List com.es.service.evralarm.EvrAlarmCacheService.getEvrAlarmByAccountId(java.lang.String)] caches=[] | key=''EvrAlarm-'+#accountId' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@7fbfc31a'. At least one cache should be provided per cache operation.
@Cacheable注解中添加cacheNames即可
package ; import java.util.HashMap;import java.util.List;import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service; import com.es.entity.evralarm.EvrAlarm;import com.es.repository.evralarm.EvrAlarmDao; @Servicepublic class EvrAlarmCacheService { @Autowired private EvrAlarmDao evrAlarmDao; @Cacheable(cacheNames="EvrAlarms",key="'EvrAlarm-'+#accountId") public List
redis中存储的数据如下图:
2.Could not resolve type id 'com.es.xx.evralarm.EvrAlarm' into a subtype of [simple type, class java.lang.Object]: no such class found
at [Source: [B@29a6e242; line: 1, column: 60] (through reference chain: java.util.ArrayList[0])
业务服务中原代码:
@Cacheable(cacheNames="EvrAlarms",key="'EvrAlarm-'+#accountId") public List
看到一遍文档后明白了,根本原因是:两个微服务,实体类内容虽然一样,但是类路径不一样
四、使用StringRedisTemplate、RedisTemplate
进一步分析发现使用@Cacheable有问题,消息中间件收到第二条报警消息,如果业务系统没有处理第一条报警消息(redis中未删除,同样的key redis中已有一条)则redis中的信息不会更新
应该是:消息中间件每次接收消息,处理后都往redis中更新
使用RedisTemplate
使用Gson直接把要保存的List<>对象转成json再保存到redis
中间件所在服务存入redis:
package com.xx.service.evralarm; import java.util.HashMap;import java.util.List;import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service; import com.es.entity.evralarm.EvrAlarm;import com.es.repository.evralarm.EvrAlarmDao;import com.google.gson.Gson; @Servicepublic class EvrAlarmCacheService { @Autowired private EvrAlarmDao evrAlarmDao; @Autowired private StringRedisTemplate redisTemplate; public List
/**
* 根据账户ID查询最新告警信息
* */
public List
业务操作删除、同时删除redis:
public void deleteAccountEvralarm(String accountId, String evrAlarmId){ Map