java spring boot application中的数据如何使用Redis Cache存储?

How to use Redis Cache to store the data in java spring boot application?

我在 AWS 账户中已经有一个 运行 Redis 缓存实例。 如何在我的 java 代码中使用 redis 实例端点来使用 redis 实例来存储数据。

我不知道如何在 java 中开始使用 Redis 缓存。 请帮我解决这个问题。

您可以通过包含以下依赖项来使用 spring-data-redis

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

然后指定属性如下:

spring.redis.database=0
spring.redis.host="Specify URL"
spring.redis.port=6379
spring.redis.password=mypass
spring.redis.timeout=60000

然后使用RedisTemplate

@Autowired
private RedisTemplate<Long, Book> redisTemplate;

public void save(Book book) {
    redisTemplate.opsForValue().set(book.getId(), book);
}

public Book findById(Long id) {
    return redisTemplate.opsForValue().get(id);
}