简体   繁体   English

如何使用 Redis Cache 将数据存储在 java spring 启动应用程序中?

[英]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.

相关问题 如何为非 spring 启动 java 应用程序设置 redis 缓存前缀键配置1823E6CC71 - How to set redis cache prefix key for non spring boot java application using xml configuration 如何在没有spring boot java项目的情况下使用spring-data-redis - How to use spring-data-redis without spring boot java project 为什么我的 Redis 缓存不能在我的 Spring-Boot/Spring-Data 应用程序中工作? - Why isn't my Redis cache working in my Spring-Boot/Spring-Data application? 无法使用Java Spring Boot Session Data Redis在Redis中存储会话 - Unable to store session in Redis using Java Spring Boot Session Data Redis spring 引导缓存注释在使用 Redis 进行缓存的应用程序中的作用是什么? - What's the role of spring boot cache annotations in a application which use Redis for caching? 如何在Spring启动应用程序中启动应用程序期间缓存数据 - How to cache data during application startup in Spring boot application 为什么 Redis 缓存在我的 Spring 启动应用程序中没有变空? - Why Redis Cache is not getting empty in my Spring Boot application? 如何使用 Thymleaf 在 Spring Boot 应用程序中正确缓存数据 - How to properly cache data in spring boot application with Thymleaf 使用 mongodb 和 redis 作为缓存的 Spring Boot - Spring boot with mongodb and redis as cache 如何通过 Spring 启动 Spring 数据从 redis 缓存中获取所有密钥? - How do I get all the keys from a redis cache via Spring Boot with Spring Data Redis 2.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM