Java development framework spring implements custom cache tags

Since spring 3 After 1, spring introduced Abstract caching, which can cache the data returned by the method by adding @ cacheable and other tags on the method. But how is it implemented? Let's take a look at an example. First, we define a @ mycacheable

Then define the facet that handles mycacheable

The above code shows how to handle the custom tags of mycacheable and the rules for generating key values by default. Finally, the generated key value looks like this: caching aspectj. Calculator. sum(Integer=1;Integer=2;) The following code adds the mycacheable tag to the method

The mycacheable tag is added to the method. When the key value is the same, the data will be directly obtained from the cache. If there is no same key value, it will be recalculated because it is only an addition operation and takes a very short time. We're here to sleep for three seconds. We are in spring config The XML configuration is as follows:

Test class:

Let's take a look at the results of the operation:

From the results, the results are calculated directly for the first time and obtained from the cache for the second time.

The above is all about spring's implementation of custom cache tags. I hope it will be helpful to everyone's learning

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>