Spring Boot Cache ProviderThe Spring Boot framework allows the integration of various cache providers, such as EhCache, Redis, Hazelcast, Infinispan, Caffeine, etc. The cache provider allows the developer to configure cache transparently and explicitly in an application. We should use cache because it reduces the number of executions and increases the performance of the application. In Spring Boot, the cache abstraction does not provide the actual space for the cache. It depends on the abstraction that occurred by the org.springframework.cache.Cache or org.springframework.cache.CacheManager interfaces. Caching Auto-configurationThe Spring Boot Framework simplifies the implementation of caching by auto-configuration support. It searches for the libraries and configuration-files in the classpath and initializes the required dependency beans at the time of application startup. The auto-configuration of caching includes the following steps:
For example, if we want to implement EhCache in an application, first we enable the cache in the configuration file. Add EhCache dependency in the pom.xml file. It adds the required libraries in the classpath. At the end, configure the file for cache provider. Here, we are using the EhCache so need to configure ehcache.xml file at the root of the classpath. When we do not define a bean of type CacheManager or CacheResolver, the Spring Boot Framework tries to detect the following cache provider:
If the Spring Boot finds the more than one cache provider in the classpath, in such cases, we must specify the cache provider explicitly in the application.properties file. We can set up a particular cache provider by using the property spring.cache.type. It is used in a certain environment if we want to disable caching. The Spring Boot Framework provides a starter dependency that adds basic cache dependency in the application. The starter cache dependency, by default, provides the spring-context-support dependency. Note: We must include spring-context-support dependency in pom.xml file if we add cache dependency manually. Because, it provides support for Jcache, EhCache, and Caffiene.The Spring Boot Framework automatically configures the CacheManager that can be further customized by implementing the CacheManagerCustomizer interface. In the follwoig example, we have set up a flag that passes the null values to the primary map. The above bean expects a auto-configured ConcurrentMapCacheManager. If the ConcurrentMapCacheManager is not auto-configures, the customizer will not invoke in any way. We can have any number of customizer and arrange them in order by using the annotation @Order or @Ordered. Generic CachingIf the spring-context-support defines at least one org.springframework.cache.Cache bean, it uses the Generic cache. The CacheManager bundled all the beans and configured them. JCacheJCache is a self-starting process that is provided by the javax.cache.spi.CahingProvider. It is present on the classpath JSR 107. The spring-boot-starter-cache provides the JCacheCacheManager. We can add any other cache library as well. Note: Spring Boot prefers the JSR support if a cache library provides both native implementation and JSR support.EhCache 2.xEHCache is Java-based, open-source, and widely used cache. In order to use EhCache we should use the following dependency. There are two ways to configure EhCache:
EhCache used a file called ehcache.xml. If the application found the file on the classpath, the EhCacheCacheManager provided by the spring-boot-starter-cache. We can configure the XML file by using the following property: HazelcastWhen we enable the caching in an application, Spring Boot wraps the HazelcastInstance automatically in the CacheManager. It distributes the data equally among the nodes. We can configure Hazelcast by using the following property. If the property is not set, Spring Boot tries to find the hazelcast.xml (Hazelcast configuration) file on the classpath. InfinispanInfinispan is an embedded Java library. It is used as a cache or a data grid. It stores data in the key-value form. It can be easily integrated with JCache, JPA Quarkus, Spring, etc. It does not have a default file location, so we should specify it explicitly. If the infinispan is not specified explicitly, it uses default bootstrap. CouchbaseThe CouchebaseCacheManager is automatically configured when we implement couchbase-spring-cache, and Couchbase is configured. All the operations related to the cache perform in the Bucket. It allows us to create additional caches (if required) by setting up the property spring.cache.cache-name. The customizer allows us to create additional Buckets, in which we can create another cache. Let's understand the above concept through an example. Suppose that we need three caches named cacheA, cacheB, and cacheC. The cacheA and cacheB are on the main Bucket (i.e., auto-configured Bucket). The cacheC is on another Bucket that is to live for a few seconds, say 4 seconds. Hence, we can create cacheA and cacheB by specifying the property, as follows: RedisThe RedisCacheManager is autoconfigured when we configure Redis. It also allows us to create additional cache by using the property spring.cache.cache-names. The default configuration can be achieved by using the property spring.cache.redis.*. We can take full control over the default configuration by using the RedisCacheConfiguration bean. The above properties configure two caches named cacheA and cacheB, that lives for 10 minutes. CaffeineThe caffeine is a Java based caching library. It also provides an in-memory cache. The spring-boot-starter-cache dependency automatically configures the CaffeineCacheManger, if it founds the Caffeine in the classpath. If we want to use Caffeine in an application, we need to add the following dependency: The caffeine cache allows us to define size and time to live of the cache by using the property spring.cache.caffeine.spec. For example: The above configuration creates two caches named cache1 and cache2. The maximum size of the cache is 500 and the maximum time to live cache is 6 seconds. SimpleIt is the default implementation. If no cache provider is specified. It configures a ConcurrentHashMap as a cache store if the Spring Boot does not find any cache provider in the classpath. For example, if we want two caches, set their name by using the following property: NoneWhen we enable the cache by using the annotation @EnableCaching, the application expects a suitable configuration. It is used when we want to disable the cache in a certain environment. We use the property spring.cache.type to disable the cache. Next TopicSpring Boot EhCaching
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com