Springboot Support for caching

Source: Internet
Author: User
Tags deprecated redis infinispan hazelcast
springboot support for caching

Caching is an abstraction that relies on the implementation of Org.springframework.cache.Cache and Org.springframework.cache.CacheManager interfaces. CacheManager as long as the cache support is enabled through the @enablecaching annotation, Spring boot automatically configures the appropriate configuration according to the implementation. If you are using a cache infrastructure that is not an interface-based bean, be sure to enable the Proxytargetclass property @enablecaching.

If you have not defined the type CacheManager or cacheresolver named Bean cacheresolver (see Cachingconfigurer), in the spring In boot, the appropriate cache manager (CacheManager) is configured with @enablecaching annotation automation, and Spring boot detects the cache provider according to the following sequence:

1.Generic


2.JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, etc)


3.EhCache 2.x


4.Hazelcast


5.Infinispan


6.Couchbase


7.Redis


8.Caffeine


9.Guava (deprecated)


10.Simple

You can also force the cache provider to use the Spring.cache.type property. Use this property if you need to completely disable caching in some environments, such as tests.

If CacheManager is automatically configured by spring boot, you can further adjust its cachemanagercustomizer configuration by exposing the bean that implements the interface before it is fully initialized. The following settings are used for the cache name.

@Bean

Public cachemanagercustomizer<concurrentmapcachemanager>

Cachemanagercustomizer () {

Returnnew cachemanagercustomizer<concurrentmapcachemanager> () {

@Override publicvoidcustomize (Concurrentmapcachemanager cachemanager) {cachemanager.setcachenames (Array        S.aslist ("One", "one", "one")); }    };} Generic

If the context defines at least one org.springframework.cache.Cache bean, use the generic cache to configure the CacheManager wrapper. Jcache

Jcache through the class Javax.cache.spi.CachingProvider path (i.e. a cache library that conforms to JSR-107) and "Starter" Jcachecachemanager Provides the boot Spring-boot-starter-cache. There are a variety of compatible libraries, and Spring boot provides dependency management for Ehcache 3,hazelcast and Infinispan. You can also add any other compatible library.

There may be multiple providers present, in which case the provider must be explicitly specified. Spring boot can accommodate implementation details even if the JSR-107 standard does not enforce a standardized way to define the location of the configuration file.

Spring.cache.jcache.provider=com.acme.mycachingprovider

Spring.cache.jcache.config = classpath:acme.xml EhCache 2.x

If you find a file named under the root directory of the classpath ehcache.xml, use Ehcache 2.x. If Ehcache 2.x Ehcachecachemanager is provided by Spring-boot-starter-cache "starter" and such files exist, it is used to boot the cache manager. You can also provide an alternate configuration file using the following methods:

Spring.cache.ehcache.config=classpath:config/another-config.xml Hazelcast

Spring Boot has general support for Hazelcast. If a hazelcastinstance is already configured automatically, it will be automatically wrapped in a cachemanager.

If for some reason you need a different hazelcastinstance cache, you can request spring boot to create a separate, only for CacheManager

Spring.cache.hazelcast.config =

Classpath:config/my-cache-hazelcast.xml

If you create a separate hazelcastinstance this way, it is not registered in the application context. Redis

If Redis is available and configured, Rediscachemanager is configured automatically. You can also use the Spring.cache.cache-names property to create additional caches at startup.

By default, a key prefix is added to prevent Redis from having overlapping keys if two separate caches use the same key, and may return invalid values. If you create your own, we strongly recommend that you enable this setting Rediscachemanager. Guava Deprecated

If guava Guavacachemanager is present, it is configured automatically. You can use the Spring.cache.cache-names property to create the cache at startup and customize the cache in this order:

A cache specification definition
1. Spring.cache.guava.spec
1. Com.google.common.cache.CacheBuilderSpec defines a bean
1. Com.google.common.cache.CacheBuilder defines a bean
For example, the following configuration creates Foo and bar cache according to 500 maximum size and survival time of 10 minutes

spring.cache.cache-names=foo,barspring.cache.guava.spec=maximumsize=500,expireafteraccess=600s

In addition, if Com.google.common.cache.CacheLoader defines a bean, it is automatically associated to Guavacachemanager. Since Cacheloader will be associated to all caches managed by the cache manager, it must be defined as Cacheloader simple

If none of these options are implemented, the configuration uses a simple implementation of the CONCURRENTHASHMAP cache store. If you do not have a cache library in your application, this is the default value.

Author: Wait for the Wind de sail

Original: http://blog.csdn.net/L_Sail/article/details/70306903

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.