0

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.

asked Nov 25, 2021 at 18:33
1

1 Answer 1

2

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

Then 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);
}
answered Nov 25, 2021 at 18:49
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @shrm.
I am trying this method which you given. In the application.properties what I have to fill in spring.redis.host="Specify URL", spring.redis.password=mypass. Which hostname and password I have to add here?
@Kakashi The property name should give you a hint.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.