简体   繁体   中英

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

I have already a running instance of Redis Cache in AWS Account. How can I use the redis instance using the redis instance Endpoint in my java code to store the data.

I don't have any idea how to start with Redis Cache in java. Please help me out to resolve this.

You can use spring-data-redis by including following dependency.

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

Then specify properties as below:

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

The using 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);
}

You can use way of @shrm in previous answer, or if you wish, there is redis client for java: https://github.com/redis/jedis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM