Giter Club home page Giter Club logo

caching-notes's Introduction

caching-notes

Demo project with notes that implements caching

What is caching

  • Caching is used to reduce the load in database and network call because it will first check in cache if the same data is requested instead of multiple call in database and network call. Thus reducing the database and network calls which is a slow.

In memory caching

  • Data is stored in RAM.
  • Redis is widely known with this type of caching

Database Caching

  • Hibernate has first level caching by default

Annotations

  • @Cacheable: = Get
  • @CacheEvict: = Delete
  • @CachePut: = Put
  • @Caching: grouper like @Mappings in mapstruct

To use caching

  1. Annotate your applivation with @EnableCaching
  2. Annotate your methods with appropriate Caching Annotations.

Caching annotation attributes

  • value: specify a name for the cache
  • key: defines how each entry of cache will be define uniquely.
  • condition: defines a predicate based on method argument if true it will be cached else will not be cache.
  • unless: defines a predicate based on method returned value if true will be not cached else will be cache.
  • keyGenerator defines a user define key generator instead of using the spring default key generator which is the method parameter will be used as key.
  • allEntries if sets to true all entries with the same name will be deleted.

Difference between spring cache and hibernate L2 cache

Hibernate L2 Cache

  • Its mainly works in repository layer. caching the Model layer, the database operations accross SessionFactory. Why its called L2 cache because the L1 cache by hibernate is enabled by default and cannot be disabled its caching the current session only transaction after that when you create another session it the cached will not be accessible. Here's when L2 caching comes in, what L2 caching do is caching the database operation in SessionFactory level which means that all cache are accessible between different sessions.

Spring caching

  • Its mainly works in service layer. caching the DTO layer, and uses the 4 cache annotation above.

When to implement caching

  • To the models that has high read and low write ratio, meaning that caching best performs when the model is frequently access and rarely changing data. Why? its because saving, update and deleting data in cache is such a mess many things can happen and many aspect you should consider thats why its best use is reading.

Useful Links

Hibernate 2nd level caching 1
Hibernate 2nd level caching 2
Service level caching 1
Service level caching 2
Redis installation

caching-notes's People

Contributors

elleined avatar

Stargazers

Jc Abalos avatar

Watchers

 avatar

caching-notes's Issues

Service level caching

Service level caching

Dependencies

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.