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

缓存-Redis

Springboot使用Redis

  1. 引入pom依赖:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  2. 在application.yml、application-dev.yml中配置Redis的访问信息

    spring:redis:host: ${sky.redis.host}port: ${sky.redis.port}password: ${sky.redis.password}database: ${sky.redis.database}
    
    sky:redis:host: localhostport: 6379password: foobareddatabase: 0
    

    其中,本机redis的password可以从/usr/local/etc/redis.conf获取(如果使用Homebrew安装的话),这个配置文件中有一行代码为

    requirepass foobared
    

    则password就是foobared

  3. 增加Redis配置类,实际上是封装RedisTemplate初始化的过程

    @Configuration
    @Slf4j
    public class RedisConfiguration {@Beanpublic RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){log.info("RedisTemplate初始化");RedisTemplate redisTemplate = new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());return redisTemplate;}
    }
    
  4. Redis缓存的使用一般伴随着查询数据库的操作,即在Redis中缓存一些热点数据,减少查数据库的流量。下面是进行Redis缓存的过程,核心就是调用了redisTemplate.opsForValue().get/set方法。

    @RestController("userDishController")
    @RequestMapping("/user/dish")
    @Slf4j
    public class DishController {@Autowiredprivate DishService dishService;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@GetMapping("/list")@SuppressWarnings("unchecked")public Result<List<DishVO>> list(Long categoryId){// 在redis中查询String cacheKey = "dish_" + categoryId;List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(cacheKey);if(list != null && !list.isEmpty()){return Result.success(list);}// 从数据库中查询Dish dish = new Dish();dish.setCategoryId(categoryId);dish.setStatus(StatusConstant.ENABLE);list = dishService.listWithFlavor(dish);// 放入redis缓存redisTemplate.opsForValue().set(cacheKey, list);return Result.success(list);}
    }
    
  5. 缓存的目标是要和数据库保持一致,因此当数据库发生变更时,要及时清理缓存,也就是调用redisTemplate.delete的方法;在调用这个方法时,可以先从Redis中查询所有符合条件的key,查询key是可以通过通配符*进行匹配的。如下面代码就是删除所有dish_开头的缓存项。

    /*** 删除菜品** @param ids* @return*/@DeleteMapping("/delete")public Result<String> delete(@RequestParam List<Long> ids) {log.info("删除菜品:{}", ids);dishService.deleteBatch(ids);clearRedis("dish_*");return Result.success();}/*** @param keys: dish_** @return void* @author jiayueyuanfang* @description 只在这个类使用,使用private私有方法* @date 2023/12/23 11:53*/private void clearRedis(String pattern) {Set<String> cacheKeys = redisTemplate.keys(pattern);redisTemplate.delete(cacheKeys);
    }
    

    Redis适用场景:适合热点数据存储:数据量小,对延迟敏感的情况

SpringCache操作缓存

Spring Cache是Spring框架提供的一个抽象模块,它提供了一种通用的缓存抽象,可以让你在不改变应用代码的前提下,添加和切换不同的缓存实现。

Spring Cache主要提供了以下几个注解来支持缓存操作:

  1. @Cacheable:应用在读取数据的方法上,用于将结果放入缓存。第一次调用时会执行方法并将结果放入缓存,之后的调用会直接从缓存中获取数据,不再执行方法。

  2. @CachePut:应用在写数据的方法上,无论什么情况,都会执行方法,并将结果放入缓存。

  3. @CacheEvict:应用在移除数据的方法上,用于从缓存中移除相应的数据。

  4. @Caching:组合注解,可以同时应用多个其他的缓存注解。

  5. @CacheConfig:类级别的注解,用于共享缓存的设置,比如缓存的名字。

Spring Cache并不直接实现缓存,而是提供了一套缓存抽象,你可以根据需要选择合适的缓存实现,比如EhCache、Guava Cache、Caffeine、Redis等。代码运行时,Spring Cache会生成代理来操作对应的缓存实现。下面介绍下操作Redis的情况。

  1. 如果操作Redis,需要引入pom依赖

    // 引入Spring Cache
    <dependency><groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    // 引入redis依赖
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  2. 启动类上增加注解:@EnableCaching // 开启SpringCache

    @Slf4j
    @SpringBootApplication
    @EnableCaching // 开启SpringCache
    public class CacheDemoApplication {public static void main(String[] args) {SpringApplication.run(CacheDemoApplication.class,args);log.info("项目启动成功...");}
    }
    
  3. 在controller查询方法上分别使用上述注解

    package com.itheima.controller;import com.itheima.entity.User;
    import com.itheima.mapper.UserMapper;
    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.web.bind.annotation.*;@RestController
    @RequestMapping("/user")
    @Slf4j
    public class UserController {@Autowiredprivate UserMapper userMapper;@PostMapping
    //    @CachePut(cacheNames = "userCache", key="#user.id")@CachePut(cacheNames = "userCache", key="#result.id")public User save(@RequestBody User user){userMapper.insert(user);return user;}@DeleteMapping@CacheEvict(cacheNames = "userCache", key = "#id")public void deleteById(Long id){userMapper.deleteById(id);}@DeleteMapping("/delAll")public void deleteAll(){userMapper.deleteAll();}@GetMapping@Cacheable(cacheNames = "userCache", key = "#id")public User getById(Long id){User user = userMapper.getById(id);return user;}}
  4. 使用SpringCache最方便的地方在于,可以自由切换缓存类型,比如想切换成本地缓存CaffeineCache(GuavaCache的替代品,Spring5.1以后不再支持GuavaCache),仅需要在pom依赖里面把对Redis的依赖更改为CaffeineCache,同时增加CaffeineCache的配置类即可

     <dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.8.8</version></dependency>
    
    @Configuration
    @EnableCaching
    public class CacheConfig {@Beanpublic CacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager();cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES));return cacheManager;}
    }
    

    其他代码不需要变更

注意:如果pom中引入了多个缓存的依赖,则SpringCache无法辨别使用哪个缓存,则注解不生效

Git代码地址:Git
接口文档:http://localhost:8080/doc.html#/default/user-controller/saveUsingPOST

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

相关文章:

  • PADS Layout安全间距检查报错
  • ebpf基础篇(二) ----- ebpf前世今生
  • 我的一天:追求专业成长与生活平衡
  • 【动态规划】斐波那契数列模型
  • 机器人运动学分析与动力学分析主要作用
  • 【Java 基础】33 JDBC
  • Unity中Shader缩放矩阵
  • Nessus详细安装-windows (保姆级教程)
  • Stream流的简单使用
  • 智能优化算法应用:基于蛇优化算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • vue和react diff的详解和不同
  • 智能优化算法应用:基于鹈鹕算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • 10:IIC通信
  • 互联网上门洗衣洗鞋小程序优势有哪些?
  • Java中如何优雅地根治null值引起的Bug问题
  • C# WPF上位机开发(子窗口通知父窗口更新进度)
  • XUbuntu22.04之跨平台容器格式工具:MKVToolNix(二百零三)
  • vue中的生命周期和VueComponent实例对象
  • Hooked协议掀起WEB3新浪潮
  • 【图文教程】windows 下 MongoDB 介绍下载安装配置
  • 算法复杂度-BigO表示法
  • 测试理论知识五:功能测试、系统测试、验收测试、安装测试、测试的计划与控制
  • 太阳能爆闪警示灯
  • 怎么为pdf文件添加水印?
  • 基于ssm医药信息管理系统论文
  • Ceph存储体系架构?
  • 详解现实世界资产(RWAs)
  • Windows漏洞利用开发——利用ROP绕过DEP保护
  • 合并两个有序链表算法(leetcode第21题)
  • 二维码初体验 com.google.zxing 实现续 - web api封装