top of page
Jay Patel

Caching in Spring Boot

What is caching?

  • Caching is the process of storing and retrieving frequently used data from a high speed database layer.

  • Caching databases are in memory databases which stores data in RAM which makes them faster compared to traditional databases.

  • Traditional databases stores data in hard drives which makes them comparatively slower than caching databases.

  • Caching databases uses HashMap type data structure which provides accessing data with time complexity of O(1).



What are the advantages of using caching?

  • Reading and Writing data is faster.

  • Directing request to caching database, reduces request going to main databases which increases performance of main databases.



What are the disadvantages of using caching?

  • Caching can lead to consistency issues where data is changed in main database but haven't updated in caching.



Which are the popular caching databases?

  • Redis, Memcached, etc.



Explain caching eviction policies

As caching databases are stored in RAM we may have size limitation. Also, to avoid consistency issues we have to make cache short lived. For that, we can use several algorithm to decide which data to keep and which data to erase.

  1. Time based eviction policy - E.g. we can set cache time to live (TTL) for 15 minutes.

  2. Least recently used (LRU)

  3. Least frequently used (LFU)

  4. First in First Out (FIFO)



Explain important caching annotations

  1. @EnableCaching - To enable caching in spring boot add this annotation in the class containing main method with @SpringBootApplication.

  2. @Cacheable - This enables method to check the caching database first, if data is not found then method will get data from main database and add to the cache and return desired output.

  3. @CachePut - This enables method to update cache.

  4. @CacheEvict - This enables method to evict data from cache.

  5. @CacheConfig - This is a class level annotation which add default configuration to all method in the class with cache related annotation.


Sample project for Caching


Sample caching program


package com.miit.students.service;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import com.miit.students.models.Student;
import com.miit.students.repository.StudentRepository;


@Service
@CacheConfig(cacheNames = "student")
public class StudentServiceImpl implements StudentService {


	@Autowired
	private StudentRepository studentRepository;


	@Override
	public Student addStudent(Student student) {
		return studentRepository.save(student);
	}


	@Override
	@CachePut(key = "#id")
	public Student updateStudent(Integer id, Student student) {
		addStudent(student);
		return getStudent(id);
	}


	@Override
	@Cacheable(key = "#id")
	public Student getStudent(Integer id) {
		return studentRepository.findById(id).orElse(new Student());
	}


	@Override
	public List<Student> getAllStudents() {
		return studentRepository.findAll();
	}


	@Override
	@CacheEvict(key = "#id")
	public Boolean deleteStudent(Integer id) {
		studentRepository.deleteById(id);
		return true;
	}


}

27 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page