• Springboot自定义缓存配置 CacheManager集成redis


    引入依赖

    <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>
    <dependency>
         <groupId>redis.clients</groupId>
         <artifactId>jedis</artifactId>
         <version>3.3.0</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    application.properties中

    redis.host=192.168.1.252
    redis.port=6379
    redis.database=1
    redis.password=1234
    redis.alive=600s
    
    • 1
    • 2
    • 3
    • 4
    • 5

    redis配置类

    @Data
    @Component
    @ConfigurationProperties(prefix = "redis")
    public class RedisProperties {
    	private String host;
    
    	private String port;
    
    	private String database;
    
    	private String password;
    
    	private String alive;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    CacheManager与Redis整合配置类

    @EnableConfigurationProperties(RedisProperties.class)
    @Configuration
    @EnableCaching
    public class RedisConfig {
    	
        @Autowired
    	RedisProperties redisProperties;
    
    	 /**
         * jedis连接工厂
         * @return
         */
        @Bean
        public JedisConnectionFactory jedisConnectionFactory() {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());
            redisStandaloneConfiguration.setPort(redisProperties.getPort());
            redisStandaloneConfiguration.setHostName(redisProperties.getHost());
            redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
            JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);
            return factory;
        }
        
        @Bean
        public RedisCacheManager redisCacheManager(RedisConnectionFactory jedisConnectionFactory) {
     
            GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            // 设置过期时间和序列化类型
            RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(cacheProperties.getAlive()).computePrefixWith(cacheName -> "demoService".concat(":").concat(cacheName).concat(":")).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));
     
            return RedisCacheManager.builder(jedisConnectionFactory)
                    .cacheDefaults(config).build();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    Springboot 集成redis后使用缓存

    Service 层

    @Service
    public class StuService {
        //@Cacheable: 标记方法的结果可以被缓存,下次调用时直接从缓存中获取结果,如果缓存中不存在,则执行方法并将结果放入缓存
    	@Cacheable(value = "stus", key = "#id")
    	public Student getStuById(Long id) {
    	    // 从数据库中获取用户信息
    	    return stuRepository.findById(id);
    	}
    
        //@CachePut: 标记方法的结果可以被缓存,每次调用方法都会执行方法并将结果放入缓存,适用于更新缓存数据的场景
    	@CachePut(value = "stus", key = "#stu.id")
    	public Student updateStu(Student stu) {
    	    // 更新数据库中的用户信息
    	    return stuRepository.save(stu);
    	}
    
    	//@CacheEvict:标记方法会从缓存中移除指定的数据,可以通过指定的条件来决定移除哪些缓存数据
    	@CacheEvict(value = "stus", key = "#id")
    	public void deleteStuById(Long id) {
    	    // 从数据库中删除学生信息
    	    stuRepository.deleteById(id);
    	}
    
    	//@Caching:可以同时使用多个缓存注解,用于组合多个缓存操作
    	@Caching(
    	    cacheable = {
    	        @Cacheable(value = "stus", key = "#id")
    	    },
    	    evict = {
    	        @CacheEvict(value = "stus", key = "#stu.id")
    	    }
    	)
    	public Student getStuByIdAndUpdate(Long id, Student stu) {
    	    // 更新数据库中的学生信息
    	    return stuRepository.save(stu);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    参考博客:https://blog.csdn.net/yu619251940/article/details/130244927
    https://blog.csdn.net/yiqiu1959/article/details/126151591
    https://blog.csdn.net/weixin_45962741/article/details/120710578?spm=1001.2014.3001.5506

  • 相关阅读:
    02Halcon标定实验
    使用OpenVINOTM预处理API进一步提升YOLOv5推理性能
    【【萌新的FPGA学习之按键控制LED实验】】
    OTP语音芯片 NV080D在智能空气检测仪的应用
    RTSP/Onvif安防视频平台EasyNVR级联至EasyNVS系统不显示通道,是什么原因?
    C++实现Wlan自动连接(wpa2 enterprise)
    Highlight Insights with Conditional
    Ubuntu磁盘扩容
    2022.9 PAT甲级题解
    ftp多用户多目录配置
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/132832001