springboot——集成redis cache

一、pom.xml配置

	<dependencies>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9.3</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

二、application.properties配置

spring.redis.host=10.6.3.148
spring.redis.port=6379
spring.redis.password=

spring.cache.redis.time-to-live=100000

三、示例代码

@SpringBootApplication
@EnableCaching
public class CacheApplication {

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

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }

    private RedisSerializer<Object> valueSerializer() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        return new GenericJackson2JsonRedisSerializer(objectMapper);
    }
}

@Data
public class User implements Serializable {
    private String id;
    private String name;
    private String age;
    private String phoneNumber;
}

@Service
public class UserManager {

    @Cacheable(value = "user", key = "#id")
    public User getUser(String id) {
        User user = new User();
        user.setId(id);
        user.setName("kk_cock");
        user.setAge("18");
        user.setPhoneNumber("13568987450");
        return user;
    }

    @CacheEvict(value = "user", key = "#id")
    public void delete(String id) {
    }

    @CachePut(value = "user", key = "#result.id")
    public User update(User user) {
        user.setName("jj");
        return user;
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheApplicationTests {
    @Autowired
    private UserManager userManager;

    @Test
    public void contextLoads() {
        String id = "123456";
        User user = userManager.getUser(id);

        String id2 = "654321";
        User user2 = userManager.getUser(id2);


        userManager.update(user);

        System.out.println(userManager.getUser(id) == user);
        System.out.println(userManager.getUser(id2) == user2);
    }

}

四、执行结果

在这里插入图片描述

五、参考链接

  1. springboot使用cache缓存
  2. SpringBoot基础系列-SpringCache使用
  3. 缓存篇(三)- Spring Cache框架
  4. spring boot中的cache使用
  5. 【spring boot2】第10篇:spring boot 整合 cache
  6. Spring Cache介绍和使用
  7. 史上最全的Spring Boot Cache使用与整合
  8. spring-boot+redis实现缓存功能
  9. Spring Boot(八)集成Spring Cache 和 Redis
  10. Spring Cache扩展:注解失效时间+主动刷新缓存(二)
  11. 配置spring cache RedisCacheManager的序列化方法
  12. Spring Cache Redis 修改序列化方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值