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 no.of 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 : https://redis.io/download/#redis-downloads
-> Run 'redis-server.exe' file.
Note: By default it runs on '6379' port number
-> Run 'Redis-cli.exe' 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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
------------------------------
@Configuration
public class RedisConfig {
@Bean
public JedisConnectionFactory getJedisConnection() {
JedisConnectionFactory factory = new JedisConnectionFactory();
// factory.setHostName(hostName);
// factory.setPassword(password);
// factory.setPort(port);;
return factory;
}
@Bean
@Primary
public RedisTemplate<String, User> getRedisTemplate(JedisConnectionFactory factory) {
RedisTemplate<String, User> rt = new RedisTemplate<>();
rt.setConnectionFactory(factory);
return rt;
}
}
-----------------------------------------------------
package in.ashokit.binding;
import java.io.Serializable;
import lombok.Data;
@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 = redisTemplate.opsForHash();
}
@PostMapping("/user")
public String storeData(@RequestBody User user) {
hashOps.put("PERSONS", user.getUid(), user);
return "success";
}
@GetMapping("/user/{uid}")
public User getData(@PathVariable Integer uid) {
User value = (User) hashOps.get("PERSONS", uid);
return value;
}
@GetMapping("/users")
public List<User> getAllUsers(){
return hashOps.values("PERSONS");
}
@DeleteMapping("/user/{uid}")
public String deleteUser(@PathVariable Integer uid) {
hashOps.delete("PERSONS", uid);
return "User Deleted";
}
}
Comments
Post a Comment