SPRINGAOP implementing Redis Cache and MySQL database synchronization

Source: Internet
Author: User
Tags aop getmessage

1, define a slice, useafterreturningnotifications, modifications, deletions, additions, etc. to update the cache after success

A, modify the database data first, modify the success and then sync to the cache,

b, delete the database data first, delete the corresponding data in the cache and then clean up

C, insert the database first, insert the success, and then sync into the cache

Com.aspect.demo;com.model.company;com.model.exceptioncompany;com. Cache.rediscache;org.aspectj.lang.joinpoint;org.aspectj.lang.annotation.*;org.slf4j.logger;o Rg.slf4j.LoggerFactory;; Java.util.List; The receivecachemanager {    //cache management class is used to get the unit information data in the cache     rediscache  ;    setrediscache (Rediscache rediscache)  {         .= redisCache;    }    Logger =  Loggerfactory. (Receivecachemanager.);      ()     insertservice ()  { }     ()     updateservice ()  { }     ()      DeleteService ()  { }     ()           Insertcompany (Joinpoint joinpoint)  {              {
            //Get method Parameters                   Object[] args =  Joinpoint.getargs ();                   (Args != && args.> && args[].getclass ()  ==  Company.)  {                      Company company =  (company)  args[];                       (. Exists ()) {                           object object = .get ();                           ( object == ) {}{                              List< exceptioncompany> list =  (list<exceptioncompany>)  object;                               exceptioncompany exceptioncompany = exceptioncompany ( );                              exceptioncompany.setbusinesslicense ( Company.getbusinesslicense ());                    &nbsP;         exceptioncompany.setcompanyname (company.getCompanyName ());                              exceptioncompany.setcompanyaddress ( Company.getcompanyaddress ());                               Exceptioncompany.setlinkman (Company.getlinkman ());                               exceptioncompany.setlinktel (Company.getlinktel ());                               exceptioncompany.setorganiZationcode (Company.getorganizationcode ());                               Exceptioncompany.settaxregistration (Company.gettaxregistration ());                               list.add (Exceptioncompany);                               .set (, list);                          }                      }                  }                  .info (+  (Joinpoint.gettarget (). GetClass (). GetName ()  +  + joinpoint.getsignature (). GetName ()  + );                 } (exception e)  {                  .error (,  e.getMessage ());              }          }     (pointcut=)     updatecompany (joinpoint  Joinpoint)  {        {
Get method Parameter             object[] args =  joinpoint.getargs ();             (args ! = && args.> && args[].getclass ()  == company.)  {                company  company =  (company)  args[];                  (. exists ()) {                     object object = .get ();                      ( object == ) {}{                         list<exceptioncompany> list =  (list<exceptioncompany>)   object;                          (I= ;i<list.size (); i++) {                               (List.get (i). GetId (). Equals (Company.getid ())) {                                  list.get (i). Setbusinesslicense (Company.getbusinesslicense ());                                  list.get (i). SetCompanyName (Company.getcompanyname());                                 list.get (i). Setcompanyaddress (Company.getcompanyaddress ());                                  list.get (i). Setlinkman (Company.getlinkman ());                                  list.get (i). Setlinktel (Company.getlinktel ());                                  list.get (i). SetOrganizationCode ( Company.getorganizationcode ());                                  list.get (i). Settaxregistration ( Company.gettaxregistration ());                                  ;                             }                         }                          .set (, list);                     }                 }            }             .info (+  (Joinpoint.gettarget (). GetClass (). GetName ()   + + joinpoint.getsignature (). GetName ()  + );         }   (exception e)  {             .error (,  e.getmessage ());         }    }      ()     deletecompany (joinpoint joinpoint)  {         {
Get method Parameter             object[] args =  joinpoint.getargs ();             (args ! = && args.> && args[].getclass ()  == company.)  {                company  company =  (company)  args[];                  (. exists ()) {                     object object = .get ();                      ( object == ) { }{                         list<exceptioncompany> list =  (List<ExceptionCompany>)  object;                          (I= ;i<list.size (); i++) {                               (List.get (i). GetId (). Equals (Company.getid ())) {                                  list.remove (List.get (i));                                  ;                             }                          }                         .set (, list);                     }                 }             }                     } (exception e)  {                     }    }}2, In the Application-*.xml file, addEnter the following configuration:    <!--use <aop:aspectj-autoproxy/> to turn on annotation style @aspectj support-->     <!--SPRING&NBSP;AOP Configuration   Use Cglib dynamic Agent technology to weave into enhanced--&GT;&NBSP;&NBSP;&NBSP;&NBSP;&LT;AOP: aspectj-autoproxy/>    <!--Facets-->    <bean id= " Receivecache " class=" Com.aspect.demo.ReceiveCacheManager "/>

Springaop implementing Redis Cache and MySQL database synchronization

Related Article

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.