11-Redis Cache Notes
11-Redis Cache Notes
11-Redis Cache Notes
1) What is cache
===========
Introduction
===========
-> Every application will interact with database to store and retrieve data
1) Transactional Tables
2) Non-Transactional Tables
-> 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
=======================
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) spring-boot-starter-web
2) spring-boot-starter-redis
3) project lombok
4) devtools
5) swagger & swagger-ui
====================
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 interface UserRepository {
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 {
@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;
@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;
}
}
-----------------------------------------------------------------------------------
---------------------