Spring Cache Source Analysis: (ii) CacheManager

Source: Internet
Author: User
Tags new set

I. Overview of CacheManager

If you need the spring cache to work properly, you must configure a CacheManager.

The CacheManager implementation class allows you to configure the Simplecachemanager and Concurrentmapcachemanager provided by the Spring-context itself. Or, use Rediscachemanager to store the cached content in Redis. The Rediscachemanager in the following class diagram comes from the Spring-data-redis jar package, Abstracttransactionsupportingcachemanager from the Spring-context-support jar package. This series of articles focuses on the Spring-context jar package.

Second, CacheManager interface


CacheManager simple description is used to store cache,cache for storing specific key-value values. For a chestnut: the name of a cache is "dairy factory", then the cache can be "small white" to obtain a cow called small white, "little black" is called the small black Cow.

Third, Abstractcachemanager abstract class

Abstractcachemanager provides basic operations that can inherit the Abstractcachemanager class to implement their own cachemanager if the existing CacheManager cannot satisfy the usage requirements.

Afterpropertiesset () method. The Org.springframework.beans.factory.InitializingBean interface from the implementation, called after the bean instantiation. Here the template method pattern is used, the implementation of the Loadcaches () method is given to the specific subclass, the general meaning is: here need to get the cache collection, specifically this cache collection from where, the specific cache implementation of what the class is whatever.

The GetCache (String) method. The cache corresponding to the cache name is obtained, and if the corresponding cache is not found, Getmissingcache (String) is called and the default Getmissingcache returns NULL. Give the decision to the implementation, you can create a cache, or log.

The Updatecachenames (String) method. If the cache is not empty after Getmissingcache, the Updatecachenames method is called here to update the Cachenames collection. Cachenames is a read-only set, and each update requires a new set to be recreated.

The Lookupcache (String) method. The corresponding cache is obtained based on a cache name, and if not, returns null without triggering the Getmissingcache method.

Decoratecache (Cache) method. Adding the Getmissingcache method creates an instance of the cache, the Decoratecache method is called to wrap the original cache, which should be guessed by the method name, which may be used in the decorated mode (also known as decorative mode, etc.). No concrete implementation is given here.

Let's take a look at an example of how to implement the Rediscachemanager:

The Decoratecache method in Rediscachemanager calls the Decoratecache method in the parent class Abstracttransactionsupportingcachemanager directly, if the condition is " Transaction-aware, the original cache instance is wrapped in a layer with the cache decorator class of the transaction, adding related operations.

Spring Cache Source Analysis: (ii) CacheManager

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.