11-Redis Cache Notes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

=====================================

Spring Boot with Redis Cache Integration


=====================================

1) What is cache

2) Why we need to go for cache

3) What is Local Cache

4) What is Distributed Cache

5) Redis Server Setup

6) Spring Boot application with Redis Integration

===========
Introduction
===========

-> Every application will interact with database to store and retrieve data

-> In DB we will maintain data in tables (Rows & Columns)

-> DB tables are divided into 2 types

1) Transactional Tables

2) Non-Transactional Tables

-> If our application is perfoming INSERT / UPDATE / DELETE operations in table


then it is called as Transactional Table.

-> If our application is performing only SELECT operation in the table then it is
called as Non-Transactional table.

Examples :

-> When student register, then our application will insert student details into
STUDENT_DTLS table (This is Transactional Table)

-> When student open 'Register For Demo' pop-up it will display COUNTRY Name with
Country Code to enter Mobile number. Here our application will read the data from
COUNTRY_MASTER table. In our application we don't insert / update / delete data in
COUNTRY_MASTER table (This is Non-Transactional table)

-> If we retrieve contries data from table for multiple times also the data will be
same and un-necessarily multiple queries will execute and it reduces our
application performance.

-> When we have this kind of scenario then its better to go for Cache Memory.

-> Static tables data we should retrieve only one time and store into Cache memory.

-> When our application needs that static we can retrieve from Cache instead of
Retrieving from DB.

-> Cache will be created in JVM and our application is also running in same JVM
hence retrieval will be faster where as DB calls are costly

===============
What is Cache ?
===============

-> Cache is a memory which is used to store the data in key-value format.

-> If our application having a requirement to use satic data then it is highly
recommended to use Cache in that application.

-> Using cache data we can reduce no.of db calls and we can improve performance of
the application.

-> If we want to use cache in one application then we will go for Local cache.
Local cache means we will implement with in our application.

-> If we want to use cache data in several applications then we will go for Global
Cache.

=======================
How to implement cache
=======================

-> Once application got started take data from db and store that into cache in the
form of key-value pair.

-> When application needs that data take it from cache memory instead of making db
call.

-> By using cache memory we can reduce no.of db calls hence application performance
will be improved.

==============================================
Q) What is Local cache & What is distributed cache ?
==============================================

-> If we implement cache with in one application then it called as local cache.

-> If we want to use cache data in multiple applications then we should go for
distributed cache (Ex: Redis cache)

=======================
Q) What is Redis?
=======================

-> Redis is an open source cache which is used to store the data in the form key-
value pair.

-> Multiple applications can connect to Redis cache to access the data.

=======================
Redis Environment Setup
=======================

1) Download redis-server software

2) Extract downloaded zip file


3) Double click on redis-server.exe file (redis server will start)

4) Double click on redis-cli.exe file

5) Type ping in client cmd it will respond with PONG.

Note : With this redis server setup is ready.

Note: In realtime, redis-server will be installed in remote machine and they will
share redis-server details (url, uname, pwd and port)

==============================================
Spring Boot Application with Redis Integration
==============================================

1) Create Spring Boot application with below dependencies

1) spring-boot-starter-web
2) spring-boot-starter-redis
3) project lombok
4) devtools
5) swagger & swagger-ui

2) Configure JedisConnectionFactory bean with Redis Server details & inject


JedisConnectionFactory bean into RedisTemplate bean

3) Create Entity class

4) Create Repository Class with required methods. Inject RedisTemplate into


Repository bean and get HashOperations object to perform redis operations.

5) Create Rest Controller with required methods

6) Create Swagger Config class

7) Configure Server port

8) Run the application and test it.

====================
pom.xml dependencies
====================

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

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
----------------------------------------------------------------------------------
package com.tekleads;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import com.tekleads.model.User;

@SpringBootApplication
public class SpringRedisExampleApplication {

@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jcf = new JedisConnectionFactory();
//jcf.setHostName(hostName);
//jcf.setPassword(password);
//jcf.setPort(port);
return jcf;
}

@Bean
RedisTemplate<String, User> redisTemplate() {
RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}

public static void main(String[] args) {


SpringApplication.run(SpringRedisExampleApplication.class, args);
}
}
----------------------------------------------------------------------------------
@Data
public class User implements Serializable {

private String id;


private String name;
private Long salary;

}
----------------------------------------------------------------------------------
public interface UserRepository {

void save(User user);


Map<String, User> findAll();

User findById(String id);

void update(User user);

void delete(String id);


}
---------------------------------------------------------------------------------
package com.tekleads.repository;

import java.util.Map;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.tekleads.model.User;

@Repository
public class UserRepositoryImpl implements UserRepository {

private HashOperations<String, String, User> hashOperations;

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


hashOperations = redisTemplate.opsForHash();
}

@Override
public void save(User user) {
hashOperations.put("USER", user.getId(), user);
}

@Override
public Map<String, User> findAll() {
return hashOperations.entries("USER");
}

@Override
public User findById(String id) {
User user = (User) hashOperations.get("USER", id);
return user;
}

@Override
public void update(User user) {
save(user);
}

@Override
public void delete(String id) {
hashOperations.delete("USER", id);
}
}
----------------------------------------------------------------------------------
package com.tekleads.rest;

import java.util.Map;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tekleads.model.User;
import com.tekleads.repository.UserRepository;

@RestController
public class UserResource {

@Autowired
private UserRepository userRepository;

@PostMapping(value = "/user", consumes = "application/json")


public User add(@RequestBody User user) {
userRepository.save(user);
return userRepository.findById(user.getId());
}

@PutMapping("/user/{id}/{name}")
public User update(@PathVariable("id") final String id, @PathVariable("name")
final String name) {
userRepository.update(new User(id, name, 1000L));
return userRepository.findById(id);
}

@DeleteMapping("/user/{id}")
public Map<String, User> delete(@PathVariable("id") final String id) {
userRepository.delete(id);
return all();
}

@GetMapping("/users")
public Map<String, User> all() {
Map<String, User> findAll = userRepository.findAll();
return findAll;
}
}

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy