SpringAOP实现redis缓存和mysql数据库同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringAOP实现redis缓存和mysql数据库同步相关的知识,希望对你有一定的参考价值。

1、定义一个切面,使用AfterReturning通知,修改、删除、新增等成功后更新缓存

    a、修改时先改数据库数据,修改成功后再同步到缓存中、

    b、删除时先删除数据库数据,删除成功再清理缓存中的对应数据

    c、新增时先插入数据库,插入成功,再同步进入缓存

com.aspect.demo;

com.model.Company;
com.model.ExceptionCompany;
com.Cache.RedisCache;
org.aspectj.lang.JoinPoint;
org.aspectj.lang.annotation.*;
org.slf4j.Logger;
org.slf4j.LoggerFactory;
;
java.util.List;

ReceiveCacheManager {


    //缓存管理类用来获取缓存中的单位信息数据
    RedisCache ;

    setRedisCache(RedisCache redisCache) {
        .= redisCache;
    }

    Logger = LoggerFactory.(ReceiveCacheManager.);
    ()
    insertService() { }

    ()
    updateService() { }

    ()
    deleteService() { }
    ()
         insertCompany(JoinPoint joinPoint) {
             {
            //获取方法参数
                 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());
                             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) {
        {
//获取方法参数

            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) {
        {
//获取方法参数

            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、在application-*.xml文件中加入如下配置:
    <!--使用<aop:aspectj-autoproxy/>来开启注解风格的@AspectJ支持-->
    <!--Spring AOP的配置 使用CGLib动态代理技术织入增强-->
    <aop:aspectj-autoproxy/>
    <!--切面-->
    <bean id="receiveCache" class="com.aspect.demo.ReceiveCacheManager"/>

以上是关于SpringAOP实现redis缓存和mysql数据库同步的主要内容,如果未能解决你的问题,请参考以下文章

redis缓存机制和底层实现

redis实现mysql的数据缓存

redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)

redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)

redis缓存服务器(Nginx+Tomcat+redis+MySQL实现session会话共享)

redis缓存服务器(Nginx+Tomcat+redis+MySQL实现session会话共享)