[英]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.我已经在 AWS 账户中有一个正在运行的 Redis 缓存实例。 How can I use the redis instance using the redis instance Endpoint in my java code to store the data.如何在我的 java 代码中使用 redis 实例端点使用 redis 实例来存储数据。
I don't have any idea how to start with Redis Cache in java.我不知道如何从 java 中的 Redis 缓存开始。 Please help me out to resolve this.请帮我解决这个问题。
You can use spring-data-redis
by including following dependency.您可以通过包含以下依赖项来使用spring-data-redis
。
<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
使用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您可以在上一个答案中使用@shrm 的方式,或者如果您愿意,还有 java 的 redis 客户端: https// github.com/redis
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.