8.10 用redis实现缓存功能和Spring Cache

这篇具有很好参考价值的文章主要介绍了8.10 用redis实现缓存功能和Spring Cache。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

什么是缓存?
缓存(Cache), 就是数据交换的缓冲区,俗称的缓存就是缓冲区内的数据,一般从数据库中获取,存储于本地代码。

通过Redis来缓存数据,减少数据库查询操作;
8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring
逻辑
每个分类的菜品保存一份缓存数据
数据库菜品数据有变更时清理缓存数据

如何将商品数据缓存起来。

   @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {

        //查询redis里面是否存在数据类;
    String key="dish_"+categoryId;
        //如果存在直接返回
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);

     if (list!=null&&list.size()>0){
         return Result.success(list);
     }
        //不存在需要查询数据库,并保存至redis里面
        Dish dish = new Dish();
        dish.setCategoryId(categoryId); //设置套餐的id

        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

         list = dishService.listWithFlavor(dish);

        redisTemplate.opsForValue().set(key,list); //将他缓存起来
        return Result.success(list);

    }

8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring
控制台没有sql了,说明缓存已经实现了。

二 数据内容发生改变的时候,需要修改redis的内容。

修改操作、删除菜品、起售或者停售、新建菜品也需要缓存数据

private void  cleanCache(String pattern){

        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys); //支持删除集合的

    }

删除对应的缓存数据

缓存套餐功能
spring Cache 实现了基于注解的缓存功能
8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring

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

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

对应的maven坐标

注解开发
8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring
Cacheable 在方法执行前查询缓存是否有数据;
8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring
8.10 用redis实现缓存功能和Spring Cache,缓存,redis,spring文章来源地址https://www.toymoban.com/news/detail-647258.html

到了这里,关于8.10 用redis实现缓存功能和Spring Cache的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Spring Cache框架(缓存)

    1、介绍: Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单加个注解,就能实现缓存功能。它提供了一层抽象,底层可以切换不同的cache实现。具体就是通过 CacheManager 接口来实现不同的缓存技术。 针对不同的混存技术需要实现不同的 CacheManage r: CacheManager 描述

    2024年02月11日
    浏览(16)
  • 【springboot】Spring Cache缓存:

    【springboot】Spring Cache缓存:

    一、导入Maven依赖: 二、实现思路: 三、代码开发:

    2024年02月11日
    浏览(16)
  • 简述Spring Cache缓存策略

    Spring框架提供了一种名为Spring Cache的缓存策略。Spring Cache是一种抽象层,它提供了一种方便的方式来管理缓存,并与Spring应用程序中的各种缓存实现(如EhCache、Guava、Caffeine等)集成。 Spring Cache使用注解(如@Cacheable、@CachePut、@CacheEvict等)来描述与缓存相关的操作。这些注解

    2024年02月10日
    浏览(14)
  • Spring Boot 缓存 Cache 入门

    Spring Boot 缓存 Cache 入门

    在系统访问量越来越大之后,往往最先出现瓶颈的往往是数据库。而为了减少数据库的压力, 我们可以选择让产品砍掉消耗数据库性能的需求 。 当然也可以引入缓存,在引入缓存之后,我们的读操作的代码,往往代码如下: 这段代码,是比较常用的缓存策略,俗称**“被动写

    2024年02月15日
    浏览(12)
  • 【SpringBoot篇】使用Spring Cache高效处理缓存数据

    【SpringBoot篇】使用Spring Cache高效处理缓存数据

    Spring Cache是一个框架,只要简单加一个注解,就能实现缓存功能。Spring Cache是Spring Framework提供的一个模块,它为应用程序添加了缓存支持。通过使用Spring Cache,你可以在方法级别上定义缓存规则,将方法的返回结果缓存起来,以提高方法调用的性能和响应速度。 是一个框架,只要简

    2024年02月05日
    浏览(30)
  • Spring Boot 3.0系列【25】数据篇之Spring Cache缓存技术使用详解

    有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot版本3.0.5 源码地址:https://gitee.com/pearl-organization/study-spring-boot3

    2023年04月14日
    浏览(11)
  • spring boot整合cache使用Ehcache 进行数据缓存

    spring boot整合cache使用Ehcache 进行数据缓存

    之前的文章 spring boot整合 cache 以redis服务 处理数据缓存 便捷开发 带着大家通过spring boot整合了 cache 缓存 那么 我们就来说说 其他服务的缓存 而spring boot默认的缓存方案就是 cache 用simple模式 spring boot的强大在于它的整合能力 它将其他缓存技术整合 统一了接口 简单说 所有的

    2024年02月19日
    浏览(12)
  • Springboot—Spring Cache 缓存方案详解及代码-Ehcache

    Spring从3.1开始定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术; 并支持使用 JCache(JSR-107) 注解简化我们开发。 常用的缓存实现有 RedisCache 、EhCache、ConcurrentMapCache 、Guava Cache( 谷歌) 等。 Spring Cache是一个框架,实现了基于注解的

    2024年02月03日
    浏览(12)
  • Spring Boot 3.2项目中使用缓存Cache的正确姿势!!!

    Spring Boot 3.2项目中使用缓存Cache的正确姿势!!!

    你是否曾想过为什么在 Spring Boot 应用中缓存是如此重要?答案在于它通过减少数据检索时间来提高性能。在本文中,我们将深入探讨缓存对微服务模式的影响,并探讨根据操作易用性、速度、可用性和可观测性等因素选择正确缓存的重要性。我们还将探讨如何最大程度地提高

    2024年02月05日
    浏览(13)
  • Spring Cache的介绍以及怎么使用(redis)

    Spring Cache的介绍以及怎么使用(redis)

    1、Spring Cache介绍 Spring Cache 是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能. Spring Cache提供了一层抽象,底层可以切换不同的cache实现。具体就是通过 CacheManager 接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口

    2024年02月11日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包