Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Cache abstraction in Spring framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about how to understand Cache abstraction in the Spring framework, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Brief introduction

Starting with version 3. 1, Spring Framework provides support for transparently adding caches to existing Spring applications. Similar to transaction support, caching abstraction allows consistent use of various caching solutions with minimal impact on code.

Since Spring 4. 1 caching abstraction has been significantly improved with JSR-107 annotations and support for more custom options.

Cache and buffer

In terms of terminology, "buffer" and "cache" can be replaced with each other. But they do represent different things.

Traditionally, an buffer is used as a temporary storage between fast data and slow data. Because the fast side needs to wait for the slow side (which affects performance), buffer relieves the pressure by allowing the whole piece of data to move at once rather than a small piece of data bit by bit. This data will only be written or read out from buffer once. In addition, the buffers can be seen by the general opposite party.

Cache, on the other hand, is defined, hidden, and not made aware of its existence. It also improves performance, but this is achieved by allowing the same data to be read multiple times at the same time.

two。 Cache abstraction

The core of caching abstraction is to apply caching to Java methods, thus reducing the number of executions based on the information available in the cache. That is, each time the target method is called, the abstraction applies the caching behavior to check whether the method has been executed for the given parameter. If so, the cached result is returned without having to execute the actual method; if not, the method is executed, cached and returned to the user so that the cached result is returned the next time the method is called. In this way, an expensive method (CPU or IO binding) can only be executed once for a given set of parameters, and the result can be reused without actually executing the method again. The cache logic is applied transparently without any interference to the caller.

Cache abstraction provides other cache-related operations, such as updating the contents of the cache or deleting one or all of them. These operations can be useful if caching changes frequently during data processing.

Like other services in Spring Framework, a caching service is an abstraction (not a caching implementation) that requires actual storage to store cached data-that is, abstraction eliminates the need for developers to write cache-related logic, but it does not provide actual data storage capacity.

Through the interface org.springframework.cache.Cache (caching)

And org.springframework.cache.CacheManager (cache manager) to realize the abstraction of cache

Some implementations of this abstraction can be used directly: JDK java.util.concurrent.ConcurrentMap-based caching (that is, the default cache is JVM-based ConcurrentMap), Ehcache 2.x MagneGemfire cache, Caffeine, and JSR-107 compliant caches (for example, Ehcache 3.x). For more information about inserting other cache stores / providers, see inserting different backend caches.

3.spring cache abstraction and multiprocess

Note that:

Spring's cache abstraction does not specifically handle multithreading or multi-processes, which are handled by the implementation of the cache.

If you have a multi-process environment (an application is deployed on multiple nodes), you need to configure the program that provides your cache accordingly. Depending on your use case, copying common data from multiple nodes is sufficient. However, if the data is modified during the application, other mechanisms are needed to notify the changes.

Caching a specialized object is equivalent to a typical get-if-not-found-then- proceed-and-put-eventually code block and is found through programming cache interaction. No locks are used, and multiple threads obtain the same data concurrently. The same is true of eviction. If multiple threads try to update or delete data concurrently, you may be using stale data. Some cache providers provide more advanced features. You can see more details in the document.

To use caching, you need to look at it in two ways:

Declaration caching: identify the methods that need to be cached and the strategies they use

Configure caching: store the data and read it into the cache.

The above is how to understand Cache abstraction in the Spring framework. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report