Demo project with notes that implements 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.
- Data is stored in RAM.
- Redis is widely known with this type of caching
- Hibernate has first level caching by default
@Cacheable
: = Get@CacheEvict
: = Delete@CachePut
: = Put@Caching
: grouper like @Mappings in mapstruct
- Annotate your applivation with @EnableCaching
- Annotate your methods with appropriate Caching Annotations.
- 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.
- 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.
- Its mainly works in service layer. caching the DTO layer, and uses the 4 cache annotation above.
- 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.
Hibernate 2nd level caching 1
Hibernate 2nd level caching 2
Service level caching 1
Service level caching 2
Redis installation