Replace the spring Boot enablecaching annotations

Source: Internet
Author: User
Tags throwable

@cacheable annotations can be used in springboot to make it easier to use Redis, an annotation that works through interceptors, when executed using the @cacheable method, to the Cglibaopproxy.java When you dynamicadvisedinterceptor.intercept a method in a location, you will find Cacheinterceptor:

Cacheinterceptor is introduced by the enablecaching annotation:

Cachingconfigurationselector:

Note in the Proxycachingconfiguration:

The return value of the method if there is a direct return to the cache in the cache results, not in the cache will actually execute the method the implementation of this function is in the Cacheinterceptor interceptor, its Invoke method:

PublicObjectInvoke(FinalMethodinvocation invocation) ThrowsThrowable {method = Invocation.getmethod (); Cacheoperationinvoker Aopallianceinvoker =New Cacheoperationinvoker () {@Overridepublic Object invoke  ()  { Span class= "Hljs-keyword" >try {return invocation.proceed ();} catch (Throwable ex) {throw new Throwablewrapper (ex);}}; try {return Execute (aopallianceinvoker, Invocation.getThis ( ), method, Invocation.getarguments ());} catch (cacheoperationinvoker.throwablewrapper th) {throw Th.getoriginal ();}}               

The specific read data is in private execute in the Execute method of the protected:

ProtectedObject Execute(cacheoperationinvoker invoker, object target, method method, object[] args) {// Check whether aspect is enabled (to cope with cases where the AJ was pulled in automatically)if (this.initialized) {class<?> Targetclass = Gettargetclass (target); collection<cacheoperation> operations = Getcacheoperationsource (). Getcacheoperations (method, Targetclass); if (! Collectionutils.isempty (Operations)) {return Execute (invoker, method, new Cacheoperationcontexts ( Operations, method, args, Target, targetclass));}} return Invoker.invoke ();}           

First of the following code first line Findcacheditem method to determine whether there is data in the cache, and then in the subsequent judgment to judge, there is the cache and directly return the results, no longer execute the interception method, or else to execute the interception method, the interception method is generally read the database, But it has nothing to do with this part of the matter.

Cache.valuewrapper CacheHit = Findcacheditem (contexts.Get (cacheableoperation.Class));Collect puts from any @Cacheable miss, if no cached item is foundlist<cacheputrequest> cacheputrequests =New Linkedlist<cacheputrequest> ();if (cachehit = null) { Collectputrequests (Contexts. Get (Cacheableoperation. Class), Cacheoperationexpressionevaluator.no_result, cacheputrequests);} Object Cachevalue;object returnvalue; if (cachehit! = null && Cacheputrequests.isempty () &&!hascacheput (contexts)) {//If there are no put Requests, just use the cache Hitcachevalue = CacheHit. get (); returnvalue = Wrapcachevalue (method, Cachevalue);} else {//Invoke the method if we don ' t have a cache Hitreturnvalue = Invokeoperation (invoker); cachevalue = Unwrapreturnvalue (returnvalue);}        

I this is the use of Redis cache, through the above code can be found, once the Redis hangs, Findcacheditem method report exception, the method of interception can not be used normally, then I need redis out of the abnormal, normal go database how to do it. My first consideration is to rewrite protected's that execute method, but found that there is a private internal class around, all of their own implementation once, completely unnecessary, because I was too lazy to tune up so I built a new interceptor, inherit Cacheinterceptor:

    @Override    protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) { try { return super.execute(invoker, target, method, args); }catch (Exception e){ return invokeOperation(invoker); } }

And then just let the new interceptor work, and I hate Springboot's annotations in this part. If you still use enablecaching annotations This is complicated, so simple and rude new annotations instead of enablecaching, and then cachingconfigurationselector need to be replaced. followed by the proxycachingconfiguration and its base class Abstractcachingconfiguration:

Rewrite this method, with the new annotation to replace the method in the enablecaching can be, because the use of online code test is not good to this paste, but the pro-test success, although the method is relatively rough, but the code in the Springboot to leave the expansion of the room, and do not want too much effort.

==========================================================

The github:https://github.com/saaavsaaa we used recently

Public Number:

                      

Replace the spring Boot enablecaching annotations

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.