SpringBoot整合缓存-SpringCache

前言

Hi, everybody!

no time long see!!

相信大家在日常开发中一定多多少少会遇到一些问题,比如本地开发,后端服务响应很快!!

可是一放到线上我们访问呢就会很慢,这时我们就会对我们的后端服务进行优化,比如增加索引使查询更快 使某个命中的机率更高 !

但是则这样我们同样要去访问数据库,这就没达到我们优化的要求。

优化:

1,增加索引

2,尽量减少数据库的查库操作

所以这时我们就会引入缓存(cache)这个概念

什么是缓存?

缓存就是数据交换的缓冲区(称作Cache),是存贮数据(使用频繁的数据)的临时地方。

当用户查询数据,首先在缓存中寻找,如果找到了则直接返回。如果找不到,则去数据库中查找。

什么是SpringCache?

由Spring提供的利用AOP实现基于注解的缓存的框架。

它进行了合理的抽象,只需要一个注解我们就能轻松实现缓存,没有繁琐的配置文件

Spring Cache支持多种缓存实现,包括内存、Redis、Memcached等。开发人员可以根据自己的需求选择合适的缓存实现。

为什么要用缓存?

  1. 提高应用程序的性能:当应用程序需要访问数据库或其他外部资源时,如果这些资源的数据经常变化,那么每次请求都需要重新获取数据,这会导致应用程序的响应时间变慢。使用缓存可以将常用的数据存储在高速缓存中,这样下一次请求相同的数据时就可以直接从缓存中获取,而不需要再次查询数据库或其他外部资源,从而提高了应用程序的性能。
  2. 减少数据库负载:当应用程序频繁地访问数据库时,会给数据库带来很大的压力,导致数据库响应变慢或者崩溃。使用缓存可以将一些常用的数据存储在高速缓存中,这样下一次请求相同的数据时就可以直接从缓存中获取,而不需要再次查询数据库,从而减少了对数据库的负载。
  3. 提高用户体验:当应用程序的响应速度变快时,用户的体验也会得到提升。使用缓存可以避免用户等待过长时间才能看到页面的变化,从而提高了用户的满意度。
  4. 支持高并发访问:当多个用户同时访问应用程序时,如果每个用户都需要访问数据库或其他外部资源,那么会对系统造成很大的负担。使用缓存可以将一些常用的数据存储在高速缓存中,这样多个用户同时访问时就可以共享缓存中的数据,从而减轻了系统的负担。

使用配置

常用注解

  • @EnableCaching:开启缓存功能,一般放在启动类上。
  • @CacheConfig:当我们需要缓存的地方越来越多,你可以使用@CacheConfig(cacheNames = {"cacheName"})注解在 class 之上来统一指定value的值
  • @Cacheable:注解表示这个方法有了缓存的功能,方法的返回值会被缓存下来,下一次调用该方法前,会去检查是否缓存中已经有值,如果有就直接返回,不调用方法。如果没有,就调用方法,然后把结果缓存起来。这个注解一般用在查询方法上。
    属性/方法名解释
    value缓存名,必填,它指定了你的缓存存放在哪块命名空间
    cacheNames与 value 差不多,二选一即可
    key可选属性,可以使用 SpEL 标签自定义缓存的key
    keyGeneratorkey的生成器。key/keyGenerator二选一使用
    cacheManager指定缓存管理器
    cacheResolver指定获取解析器
    condition条件符合则缓存
    unless条件符合则不缓存
    sync是否使用异步模式,默认为false
  • @CachePut:使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库。一般用在新增方法上。
    属性/方法名解释
    value缓存名,必填,它指定了你的缓存存放在哪块命名空间
    cacheNames与 value 差不多,二选一即可
    key可选属性,可以使用 SpEL 标签自定义缓存的key
    keyGeneratorkey的生成器。key/keyGenerator二选一使用
    cacheManager指定缓存管理器
    cacheResolver指定获取解析器
    condition条件符合则缓存
    unless条件符合则不缓存
  • @CacheEvict:使用该注解标志的方法,会清空指定的缓存。一般用在更新或者删除方法上
    属性/方法名解释
    value缓存名,必填,它指定了你的缓存存放在哪块命名空间
    cacheNames与 value 差不多,二选一即可
    key可选属性,可以使用 SpEL 标签自定义缓存的key
    keyGeneratorkey的生成器。key/keyGenerator二选一使用
    cacheManager指定缓存管理器
    cacheResolver指定获取解析器
    condition条件符合则缓存
    allEntries是否清空所有缓存,默认为 false。如果指定为 true,则方法调用后将立即清空所有的缓存
    beforeInvocation是否在方法执行前就清空,默认为 false。如果指定为 true,则在方法执行前就会清空缓存
  • .@Caching:该注解可以实现同一个方法上同时使用多种注解。

代码实例

1,引入springcache的maven依赖

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

 2,在启动类使用注解缓存功能

package com.example.cache;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@SpringBootApplication
public class SpringBootCacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCacheApplication.class, args);
    }

}

 3,配置我们的crud service接口和impl

package com.example.cache.service;

import com.example.cache.entity.User;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @Program: SpringBoot
 * @ClassName UserService
 * @Author: liutao
 * @Description: user 逻辑接口
 * @Create: 2023-11-16 00:03
 * @Version 1.0
 **/


public interface UserService {
    User save(User user);

    PageInfo<User> page(Integer page,Integer pageSize);

    User findById(Integer id);

    List<User> findAll();

    int delete(Integer id);
}
package com.example.cache.service.impl;

import com.example.cache.entity.User;
import com.example.cache.mapper.UserMapper;
import com.example.cache.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Program: SpringBoot
 * @ClassName UserServiceImpl
 * @Author: liutao
 * @Description: user 实现类
 * @Create: 2023-11-16 00:06
 * @Version 1.0
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(rollbackFor = {Exception.class})
    @Caching(put = {@CachePut(value = "userCache", key = "#user.id", unless = "#result == null")}, evict = {@CacheEvict(value = "allUsersCache", allEntries = true)})
    @Override
    public User save(User user) {
        int num = 0;
        if (user.getId() == null) {
            num = userMapper.insert(user);
        } else {
            num = userMapper.update(user);
        }
        log.info("User " + user.getId());
        return num == 1 ? userMapper.findById(user.getId()) : null;
    }

    @Cacheable(value = "allUsersCache", unless = "#result == null")
    @Override
    public PageInfo<User> page(Integer page, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        List<User> users = userMapper.findAll();
        if (users != null && !users.isEmpty()) {
            return new PageInfo<>(users);
        }
        return null;
    }

    @Cacheable(value = "userCache", key = "#id", unless = "#result == null")
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Cacheable(value = "allUsersCache", unless = "#result == null")
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Caching(evict = {@CacheEvict(value = "userCache", key = "#id"), @CacheEvict(value = "allUsersCache", allEntries = true),})
    @Override
    public int delete(Integer id) {
        return userMapper.delete(id);
    }
}

 4,web层

package com.example.cache.controller;

import com.example.cache.entity.User;
import com.example.cache.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Program: SpringBoot
 * @ClassName UserController
 * @Author: liutao
 * @Description: user api
 * @Create: 2023-11-16 00:00
 * @Version 1.0
 **/
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/page")
    Object page(Integer page,Integer pageSize) {
        return userService.page(page,pageSize);
    }

    @GetMapping("/{id}")
    User findById(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @PostMapping("/save")
    User save(User user) {
        return userService.save(user);
    }


    @DeleteMapping("/{id}")
    int delete(@PathVariable Integer id) {
        return userService.delete(id);
    }

}

 效果图

1,打开服务:访问两次 http://localhost:8080/user

2,清空日志再次访问,我们可以看到这次是从缓存查出来的,并没有进行查库操作

结尾

到这里我们的整合就结束了!!

欢迎三连和涛哥一起学习!!

see you !!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚上睡不着!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值