当前位置: 首页 > news >正文

SpringBoot开发中如何缓存数据, 减少数据库的访问频率?

一:自定义是否开启缓存

方法一:

在不同环境的配置文件中如application-dev.yml、application-test.yml、application-prod.yml,修改 spring.cache.type = none;

spring:cache:type: none	

方法二:

自定义配置

application.yml:

## 开启数据缓存
caching:enabled: true

com.scaffold.test.config.CacheConfig

缓存配置文件

@Configuration
@EnableCaching
//配置文件读取是否启用此配置
@ConditionalOnProperty(prefix = "caching", name = "enabled", havingValue = "true")
public class CacheConfig {}

二:simpleCacheManage

simpleCacheManage 基于ConcurrentHashMap 实现,不依赖其他库,如果增加了注解@EnableCaching,默认开启缓存,可以通过设置cache-names限制缓存列表

1.设置缓存列表

application.yml

spring:cache:type: simplecache-names: cache1,cache2

2.添加maven依赖:

<!-- cache 依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>

3.使用注解

注意:所有的注解是加到实现类serviceImpl方法上的

注解描述
@Cacheable在方法执行前 Spring 先查看缓存中是否有数据,若有,则直接返回缓存数据;若无数据,调用方法将方法返回值放入缓存中
@CachePut无论怎样,都会将方法的返回值放到缓存中。
@CacheEvict将一条或多条数据从缓存中删除
@Caching可以通过 @Caching 注解组合多个注解策略在一个方法

@Cacheable、@CachePut、@CacheEvict 都有 value 属性,指定的是要使用的缓存名称;key 属性指定的是数据在缓存中存储的键

4.代码实现:

4.1配置类

package com.scaffold.test.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.time.Duration;/*** 缓存配置文件* 配置文件读取是否启用此配置* @author alex*/
@Configuration
@EnableCaching
@ConditionalOnProperty(prefix = "caching", name = "enabled", havingValue = "true")
public class CacheConfig {@Beanpublic CacheManager cacheManager() {return new ConcurrentMapCacheManager("cacheData");}}

4.2实体类

package com.scaffold.test.entity;import lombok.Data;
import lombok.EqualsAndHashCode;import java.io.Serializable;/*** @author alex wong*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Student implements Serializable {private static final long serialVersionUID=1L;private int id;private String name;private Integer age;}

4.3service层

package com.scaffold.test.service;import com.scaffold.test.entity.Student;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/*** <p>* 服务类* </p>** @author alex wong*/
public interface StudentService extends IService<Student> {List<Student> findAll();Student findStudent(Student student);Student testStudent(String text);void deleteStudent(Student student);void saveStudent(Student student);}

4.4service实现类

package com.scaffold.test.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scaffold.test.entity.Student;
import com.scaffold.test.mapper.StudentMapper;
import com.scaffold.test.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;/*** <p>*  服务实现类* </p>** @author alex wong*/@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {@Resourceprivate StudentMapper studentMapper;@Override@Cacheable(value = "cacheData")public List<Student> findAll(){return studentMapper.selectAll();}/*** 缓存查询数据* @Cacheable 缓存数据到缓存 student 中* 其中缓存名称为 student 数据的 key 是 student 的 id* @param student s* @return*/@Override@Cacheable(value = "cacheData", key = "#student.id")public Student findStudent(Student student) {log.warn("增加了student为{}的数据缓存", student);int id = student.getId();if(id == 0){return null;}return studentMapper.findStudent(student);}/*** 删除缓存* @CacheEvict 从缓存 student 中删除* 其中缓存名称为 student 数据的 key 是 student 的 id* @param student s*/@Override@CacheEvict(value = "cacheData", key = "#student.id")public void deleteStudent(Student student) {log.warn("删除了student为{}的数据缓存", student);}/*** @CachePut 缓存新增的或更新的数据到缓存* 其中缓存名称为 student 数据的 key 是 student 的 id* @param student*/@Override@CachePut(value = "cacheData", key = "#student.id")public void saveStudent(Student student) {log.warn("保存了id、key 为{}的数据缓存", student);studentMapper.insertStudent(student);}@Override@Cacheable(value = "cacheData", key = "#text")public Student testStudent(String text) {System.out.println("test" + text);Student student = new Student();student.setName(text);return student;}
}

 4.5dao层接口:

package com.scaffold.test.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scaffold.test.entity.Student;import java.util.List;/*** <p>* Mapper 接口* </p>** @author alex wong*/
public interface StudentMapper extends BaseMapper<Student> {List<Student> selectAll();Student findStudent(Student student);int insertStudent(Student student);
}

4.6Mapper。xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.scaffold.test.mapper.StudentMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="com.scaffold.test.entity.Student"><result column="id" property="id"/><result column="name" property="name"/><result column="age" property="age"/></resultMap><!-- 通用查询结果列 --><sql id="Base_Column_List">id,name, age</sql><sql id="Where_Condition"><where><if test="id != null and id != ''">id=#{id}</if><if test="name != null and name != ''">and name=#{name}</if><if test="age != null and age != ''">and age=#{age}</if></where></sql><insert id="insertStudent">insert student(id, name, age)values(#{id}, #{name}, #{age})</insert><select id="selectAll" resultMap="BaseResultMap">select * from student</select><select id="findStudent" resultType="com.scaffold.test.entity.Student">select * from student<include refid="Where_Condition"></include></select>
</mapper>

4.7sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`id` int(11) NOT NULL,`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '1', 2323);
INSERT INTO `student` VALUES (2, '2', 2323);
INSERT INTO `student` VALUES (3, '3', 2323);SET FOREIGN_KEY_CHECKS = 1;

4.8Controller层

package com.scaffold.test.controller;import com.scaffold.test.entity.Student;
import com.scaffold.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** <p>*  前端控制器* </p>** @author alex wong*/
@RestController
@RequestMapping("/student")
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("list")public List<Student> getAll(){return studentService.findAll();}@GetMapping("add")public void addStudent(Student student){studentService.saveStudent(student);}@GetMapping("find")public Student findStudent(Student student){return studentService.findStudent(student);}@GetMapping("delete")public void deleteStudent(Student student){studentService.deleteStudent(student);}@GetMapping("test")public Student test(@RequestParam String text){return studentService.testStudent(text);}
}

http://www.lryc.cn/news/405595.html

相关文章:

  • PostgreSQL如何在windows/linux开启归档
  • 【启明智显分享】基于国产Model3芯片的7寸触摸屏助力智慧医疗,电子床头屏提升护理交互
  • 从理论到实践:如何用 TDengine 打造完美数据模型​
  • 可以免费合并pdf的软件 合并pdf文件的软件免费 合并pdf的软件免费
  • 【排序 滑动窗口 】1498. 满足条件的子序列数目
  • RabbitMQ普通集群搭建指南
  • AGV平面坐标系变换公式及实例
  • es切片和集群
  • IEEE官方列表会议 | 第三届能源与环境工程国际会议(CFEEE 2024)
  • 深度学习中的正则化技术 - Dropout篇
  • 《昇思 25 天学习打卡营第 18 天 | 扩散模型(Diffusion Models) 》
  • 【Django+Vue3 线上教育平台项目实战】Elasticsearch实战指南:从基础到构建课程搜索与数据同步接口
  • libtins初探-抓包嗅探
  • 大语言模型-Bert-Bidirectional Encoder Representation from Transformers
  • bug诞生记——动态库加载错乱导致程序执行异常
  • Matlab演示三维坐标系旋转
  • redis的持久化机制以及集群模式
  • 【论文解读】大模型算法发展
  • WebApi配置Swagger、Serilog、NewtonsoftJson、Sqlsugar、依赖注入框架Autofac、MD5加密
  • 【ffmpeg命令基础】视频选项讲解
  • 使用uniapp开发小程序(基础篇)
  • vue3【详解】组合式函数
  • 微服务实战系列之玩转Docker(六)
  • Python题解Leetcode Hot100之动态规划
  • 你了解GD32 MCU上下电要求吗
  • 二、【Python】入门 - 【PyCharm】安装教程
  • 2、程序设计语言基础知识
  • ARM/Linux嵌入式面经(十八):TP-Link联洲
  • 解读vue3源码-响应式篇2
  • 【测开能力提升-fastapi框架】fastapi能力提升 - 中间件与CORS