100% found this document useful (1 vote)
16 views3 pages

Using Redis Cache with Spring Boot

Cache is a temporary storage that improves application performance by reducing costly database calls. Redis is a distributed cache that stores data in key-value pairs and can be integrated with Spring Boot using specific configurations and dependencies. The document provides setup instructions for Redis and demonstrates how to create a RESTful API for user data management using Redis in a Spring Boot application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
16 views3 pages

Using Redis Cache with Spring Boot

Cache is a temporary storage that improves application performance by reducing costly database calls. Redis is a distributed cache that stores data in key-value pairs and can be integrated with Spring Boot using specific configurations and dependencies. The document provides setup instructions for Redis and demonstrates how to create a RESTful API for user data management using Redis in a Spring Boot application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

################

What is Cache ?
###############

-> Cache is a temporary storage

-> When our application wants to access same data frequently then we will use Cache
memory

-> Cache will improve performance of our application by reducing database calls

Note: Database calls are always costly which will take more time to execute

-> To reduce [Link] round trips between application and database we will use 'Cache'

##############
Redis Cache
###############

-> Redis is one of the distributed cache available in the market

-> Redis will store data in key-value pair

-> Multiple Applications can connect with Redis Cache at a time...

The open source, in-memory data store used by millions of developers as a database,
cache, streaming engine, and message broker.

############
Redis Setup
############

-> Download Redis Software

URL : [Link]

-> Run '[Link]' file

Note: By default it runs on '6379' port number

-> Run '[Link]' file

-> Type 'ping' command in Redis CLI

Note: Server willl repond with 'PONG' as response

################################
Spring Boot with Redis Integration
#################################

-> Spring Boot provided starter pom to connect with Redis Server

-> Create JedisConnectionFactory bean


-> Create RedisTemplate and Inject JedisConnectionFactory into RedisTemplate

-> Using RedisTemplate get HashOperations object

-> Using HashOperations we can perform storing/retrieving/deleting operations with


Redis Server

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>[Link]</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jedis</artifactId>
</dependency>

------------------------------

@Configuration
public class RedisConfig {

@Bean
public JedisConnectionFactory getJedisConnection() {
JedisConnectionFactory factory = new JedisConnectionFactory();
// [Link](hostName);
// [Link](password);
// [Link](port);;
return factory;
}

@Bean
@Primary
public RedisTemplate<String, User> getRedisTemplate(JedisConnectionFactory
factory) {
RedisTemplate<String, User> rt = new RedisTemplate<>();
[Link](factory);
return rt;
}

}
-----------------------------------------------------
package [Link];

import [Link];

import [Link];

@Data
public class User implements Serializable{

private Integer uid;


private String name;
private Integer age;

------------------------------------------
@RestController
public class UserRestController {

private HashOperations<String, Integer, User> hashOps;

public UserRestController(RedisTemplate<String, User> redisTemplate) {


hashOps = [Link]();
}

@PostMapping("/user")
public String storeData(@RequestBody User user) {
[Link]("PERSONS", [Link](), user);
return "success";
}

@GetMapping("/user/{uid}")
public User getData(@PathVariable Integer uid) {
User value = (User) [Link]("PERSONS", uid);
return value;
}

@GetMapping("/users")
public List<User> getAllUsers(){
return [Link]("PERSONS");
}

@DeleteMapping("/user/{uid}")
public String deleteUser(@PathVariable Integer uid) {
[Link]("PERSONS", uid);
return "User Deleted";
}
}

You might also like