Implementing caching in SpringBoot using Redis

Implementing caching in SpringBoot using Redis

what is caching?

caching is referring to a process of storing the data in a cache (a temporary storage area) which helps in the performance of an application. suppose if you want to retrieve same data from the database multiple times it leads to performance issue, to avoid this we can store the date in cache it reduces the database calls.

What is Redis?

Redis Cache is an open-source, in-memory cache that operates by storing data in key-value pairs. It is designed to facilitate fast retrieval of information, making it particularly effective for scenarios where quick access to frequently used data is essential. Checkout here Redis

Create a database and add configuration in your configuration file

server.port=9090
spring.data.redis.url=//paste the url of redis database
spring.data.redis.port=
spring.data.redis.username=/default
spring.data.redis.password=//go to refis stach copy the password

Setting up connection to a Redis server.

package in.spring.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

@Configuration
public class RedisConfig {

    @Value("${spring.data.redis.url}")
    private String url;

    @Value("${spring.data.redis.port}")
    private Integer port;

    @Value("${spring.data.redis.username}")
    private String username;

    @Value("${spring.data.redis.password}")
    private String pwd;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {

        RedisStandaloneConfiguration serverConfig = 
                new RedisStandaloneConfiguration(url, port);

        serverConfig.setUsername(username);
        serverConfig.setPassword(pwd);

        JedisClientConfiguration clientConfig = JedisClientConfiguration.builder().build();

        return new JedisConnectionFactory(serverConfig, clientConfig);
    }

}

Entity class here @RedisHash("StudentHash") ,@RedisHash is used to reprsent an entity class and StudentHash is used to save an instance of entity class named as StudentHash.

package in.spring.entity;

import org.springframework.data.redis.core.RedisHash;

@RedisHash("studentHash")
public class Student {

    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}
package in.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import in.spring.entity.Student;
import in.spring.repo.StudentRepo;

@RestController
public class AppController {
    @Autowired
    private StudentRepo srepo;

    @PostMapping("/details")
    public String addStudent(@RequestBody Student student ) {
        srepo.save(student);
        return "inserted";
    }
    @GetMapping("/")
    public Iterable<Student> getStudents() {
        return srepo.findAll();

    }


}