String Boot + Redis - SET and GET String Commands Examples


Prerequisite:

You have hands-on on the basics of how to download and setup a Spring Boot Application using the Spring Initializr tool into your IDE (any of VS Code, IntelliJ, Eclipse or Netbeans)


Also, have an understanding of adding dependencies to your project which can be Gradle or Maven based.

Step 1: Setting up your Spring Boot + Redis Project Dependencies

    Make sure you add the Redis dependency to your Spring Boot Project.


    build.gradle:

    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    }

    pom.xml:

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

Step 2: Setting Redis Configurations in application.properties


    Make sure to add the below properties if you have setup Redis Server on your local machine or a server.

    # Redis configuration
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.password=redis-db-password

    Note: If you are just starting with Redis and trying to learn, its better to setup a free Redis Cloud account and make use of it.


Step 3: Redis SET and GET Command Examples with Spring Boot


    Just like working with Spring Boot JDBCTemplate, we have a class called RedisTemplate which we can make use of to perform SET and GET commands via Java code.


    To keep the example simple, we will implement ApplicationRunner our class with @SpringBootApplication and override the run() method and write our code there.


    package com.example.redisdemo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.RedisTemplate;
    
    @SpringBootApplication
    public class RedisSetGetDemoApplication implements ApplicationRunner {
    
        @Autowired
        public RedisTemplate redisTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(RedisSetGetDemoApplication.class, args);
    
        }
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
    
            //SET Example: SET message Hello
            redisTemplate.opsForValue().set("message", "Hello");
    
            //GET Example: GET message
            System.out.println(redisTemplate.opsForValue().get("message"));
        }
    }
    

    As you would have seen in the above code, to make use of SET, we use the method opsForValue().set() from the RedisTemplate, and similarly opsForValue().get() for GET Command.


Output:

Hello

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org










Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap