一、概述

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

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. jQuery 中json字符串与对象互转

    json字符串转json对象:jQuery.parseJSON(jsonStr); json对象转json字符串:JSON.stringify(jsonObj);

  2. Node.js内置的文件系统模块(fs)

    异步读取文件 按照js的标准,异步读取一个文本文件的格式如下: 'use strict' const fs = require('fs') fs.readFile('test.txt', 'utf-8 ...

  3. visual studio + opencv + contrib

    经过一天的奋战,终把opencv给用起来了.我是用的工具是vs2017+opencv3.3 上午想用mingw+opencv,结果查了很多资料说gcc不支持opencv.我感觉很奇怪,支不支持以后再说 ...

  4. (2)Django入门

    web框架:把一个请求拆成几部分,每部分做相同的事 python中常用的框架 1.Django:大而全的框架 2.flask:微框架又叫轻量级的框架 3.Tornado:高性能框架 pycharm创建 ...

  5. Codeforces Div3 #501 A-E(2) F以后补

    感觉自己有点强迫症  不都写出来就找理由不写题解 http://codeforces.com/contest/1015   题目链接 A. Points in Segments 题目意思  n个线段  ...

  6. Python面试 【315+道题】

    Python面试 [315+道题] 第一部分 Python基础篇(80题) 为什么学习Python? 因为看到python的发展趋势,觉得需要与时俱进,第一点,python开发速度极快,能快速完成一个 ...

  7. python基础(五)——CGI编程

    使用python实现get方法和post方法传值,多选按钮,单选按钮.文本编辑区.下拉列表数据的传递,cookie的设置文件上传,文件下载.本文未经整理,仅供参考 #!/usr/bin/python ...

  8. centos7升级自带的php5.4版本到php5.6

    history命令历史 8 yum provides php #自带的只有5.4版本 9 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-relea ...

  9. JDBC事务的处理-----模拟银行转账业务

    定义: 数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成. 概要: 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: ...

  10. C++编译器报错汇总

    1.error: ‘Person’ was not declared in this scope(1)若是一个类或函数的命名空间对使用者不可见(2)成员(静态)函数没有通过对象名或类名进行调用(3)虽 ...