Recommended article reading

Click me to Read SpringBoot and caching ~ concepts, annotations, Redis integration, distributed locking

Click Me to Read SpringBoot Cache how SpringBoot cache works and how @cacheable runs

Click Me to Read SpringBoot

Click me to download

preface

To teach fishing is better than teach them to fish, suggest you to collect the article, learn to analysis \ color {red} “to teach fishing is better than teach them to fish, suggest you to collect the article, learn to analysis method to teach fishing is better than teach them to fish, suggest to collect the article, learn to analysis method Just watching cache principle, it took a long time, Won’t be able to trace the code, see a few data, can autonomous tracking \ color {red} just watching cache principle, it took a long time, still can’t tracking code, see a few data, the principle of autonomous tracking can just see the cache, it took a long time, still can’t track code, see a few data, can be followed independently

How caching works

  1. Old routines, see CacheAutoConfiguration. Java file

2. Cache configuration classes and check which classes are imported 3. Which of these configuration classes takes effect by default? Method 1. Analyze each class and click:Take the familiar redisc Configuration as an example:So by default, not the Redis cache configuration class is in effect. Method 2: Directly read the startup analysis report:


debug=true
Copy the code

Result: SimpleCacheConfiguration 4. What does positioning a SimpleCacheConfiguration do
Registered one in the container c a c h e M a n a g e r : C o n c u r r e n t M a p C a c h e M a n a g e r \ color red to attach the container registered a cacheManager: ConcurrentMapCacheManager
5, cacheManager can access and create ConcurrentMapCacheManager types of cache components, his role to save data in ConcurrentHashMap
Here is the S p r i n g B o o t This explains two things Color {# f6941D} Here is the SpringBoot cache principle, which can explain two problems

First use @ C a c h e a b l e The cache component configuration is not required at all when annotating; one is used by default m a p Color {# f6941D} When the @cacheable annotation is first used, the cache component configuration is not required at all. By default, a map is used

After each startup, the data disappears without persistence \color{# f6941D} Data will disappear after each startup, no persistence

Cacheable runs processes

@Cacheable(cacheNames = {"emp"})
	public Employee get(Integer id){
		return employeeMapper.selectById(id);
	}
Copy the code

1. Before running this method, query the Cache component (Cache component) and obtain the Cache component by the name specified by cacheName. If the Cache component does not exist for the first time, the Cache component is automatically created.
in C o n c u r r e n t M a p C a c h e M a n a g e r Hit the break point and see \ color red in ConcurrentMapCacheManager breakpoint see attach
Insert a picture description hereSelect a key from the Cache, which is the default parameter of the method. Keys are generated according to a policy, and keyGenerator is used by default. SimpleKeyGenerator is used by default to generate keys

org.springframework.cache.concurrent.ConcurrentMapCache#lookup
Copy the code

3. Target method will be called if no cache is found! ] (img – blog. Csdnimg. Cn / 20201124163…).

4. Put the result returned by the target method into the cache

If the lookUp can find it, the method is not entered

conclusion

[operation operation again operation, record record again record] \color{red} [operation operation again operation, record record again record] [operation operation again operation, record record again record] from the first tracking source code to familiar with the source code, took two days, we recommend that we must go to track four times, Don’t just look not to oh \ color {red} from tracking the source code for the first time to be familiar with the source code, spent two days time, suggest that we have to trace four don’t just look not begin from tracking the source code for the first time to be familiar with the source, spent two days time, suggest that we have to trace four don’t only look at to oh The more you know, the less you know. Hope to help you! Color {red} The more you know, the less you know! Hope to help you! The more you know, the less you know! Hope to help you!