缓存-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: localhost
        port: 6379
        password: foobared
        database: 0
    

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

    requirepass foobared
    

    则password就是foobared

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

    @Configuration
    @Slf4j
    public class RedisConfiguration {
        @Bean
        public 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 {
        @Autowired
        private DishService dishService;
        @Autowired
        private 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 {
    
        @Autowired
        private 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 {
        @Bean
        public 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

  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值