一、概述

最近在做性能优化,之前有一个业务是这样实现的:

1.温度报警后第三方通讯管理机直接把报警信息保存到数据库;

2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务逻辑,在数据库中插入“其他业务数据”;

3.前端setTimeout每隔5秒ajax去后端查询“其他业务数据”(查库);

优化后这样实现:

两个微服务,消息中间件专门一个服务,接收消息存入数据库,存入redis;业务服务直接从redis获取;

1.MQTT订阅通讯管理机报警事件主题;

2.发生报警后,java中根据报警信息保存“其他业务数据”到数据库并放入redis缓存;

3.前端setTimeout每隔5秒ajax去后端查询“其他业务数据”(改为从redis中获取);

4.下一步计划使用WebSocekt,去掉前端setTimeout;

二、使用StringRedisTemplate、RedisTemplate<String, Object>
进一步分析发现使用@Cacheable有问题,消息中间件收到第二条报警消息,如果业务系统没有处理第一条报警消息(redis中未删除,同样的key redis中已有一条)则redis中的信息不会更新。

应该是:消息中间件每次接收消息,处理后都往redis中更新

使用RedisTemplate<String, Object>直接保存List对象,redis存储中会携带一个类路径信息("com.es.xx.evralarm.EvrAlarm"),

业务服务获取的时候无法解析(两个实体类内容相同,类路径不同),只能使用StringRedisTemplate了,只能是在redis存取前后自己手动对象转json。

使用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;

@Service
public class EvrAlarmCacheService {

@Autowired
private EvrAlarmDao evrAlarmDao;

@Autowired
private StringRedisTemplate redisTemplate;

public List<EvrAlarm> getEvrAlarmByAccountId(String accountId){
Map<String,Object> params = new HashMap<>();
params.put("accountId", accountId);
params.put("limit", 1);
List<EvrAlarm> evrAlarms = evrAlarmDao.selectEvrAlarmByAccount(params);

//redis缓存
ValueOperations<String,String> vo = redisTemplate.opsForValue();
Gson gson = new Gson();
vo.set("EvrAlarm-"+accountId, gson.toJson(evrAlarms));
return evrAlarms;
}
}
业务服务从redis中取:
从redis中获取key对应的value,得到string类型的value,使用Gson转成List<>对象

/**

* 根据账户ID查询最新告警信息

* */

public List<EvrAlarm> selectEvrAlarmByAccount(String accountId){
//redis缓存中获取
ValueOperations<String,String> vo = redisTemplate.opsForValue();
String value = vo.get("EvrAlarm-"+accountId);
Gson gson = new Gson();
List<EvrAlarm> evrAlarms = gson.fromJson(value, List.class);
return evrAlarms == null ? new ArrayList<>() : evrAlarms;
}

业务操作删除、同时删除redis:

public void deleteAccountEvralarm(String accountId, String evrAlarmId){
Map<String, Object> queryMap = new HashMap<>();
queryMap.put("accountId", accountId);
queryMap.put("evrAlarmId", evrAlarmId);
accountEvralarmDao.deleteByPrimaryKey(queryMap);
//redis删除缓存
redisTemplate.delete("EvrAlarm-"+accountId);
}

SpringBoot使用redis缓存List的更多相关文章

  1. springboot整合redis缓存

    使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...

  2. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  3. SpringBoot使用redis缓存List<Object>

    一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...

  4. springboot集成redis缓存

    1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...

  5. SpringBoot整合redis缓存(一)

    准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...

  6. Java SpringBoot使用Redis缓存和Ehcache

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

  7. springboot整合redis缓存一些知识点

    前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...

  8. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  9. SpringBoot(七) - Redis 缓存

    1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...

随机推荐

  1. flask数据库的迁移

    需要的插件:flask-migrate  在每次修改模型之后,将修改的东西映射到数据库中. 使用flask-migrate 必须借助flask_scripts,这个包的所有MigrateCommand ...

  2. JetBrains PyCharm 专业版激活

    激活码获取:http://idea.lanyus.com/ JetbrainsCrack-release-enc.jar下载:提取码为1391

  3. 字符串转json格式

    方法一:var json = JSON.parse(str)方法二:eval

  4. c# 敏捷2 ForEach ToDictionary ToLookup Except比较

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  5. [Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition]

    https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n, ...

  6. Eclipse maven 错误修正方法:An error occurred while filtering resources

    最近打开Eclipse后发现项目报红叉,解决办法如下: 1.eclipse中删除该项目(注意:不要删除代码) 2.cmd,进入到项目目录下,执行命令:mvn eclipse:clean 3.重新导入项 ...

  7. Comet OJ - Contest #2 简要题解

    Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...

  8. 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。java算法

    知识点一:equalsIgnore 1.使用equals( )方法比较两个字符串是否相等.它具有如下的一般形式: boolean equals(Object str) 这里str是一个用来与调用字符串 ...

  9. 用java的io流,将一个文本框的内容反转

    import java.io.*; import java.util.ArrayList; public class test04 { public static void main(String a ...

  10. 使用terraform-provider-s3 操作minio

    尽管默认官方提供了s3 的操作,但是对于开源minio 无法支持,更多的是aws 的s3,社区提供了一个通用 s3 操作的provider(基于minio 的sdk) 环境准备 docker-comp ...