5秒钟不允许重复保存 apo 切面

package com.dnt.data.standard.server.aspect;

import com.dnt.data.standard.server.annotation.CacheLock;
import com.dnt.data.standard.server.tools.redis.cache.CacheService;
import com.dnt.data.standard.server.web.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

@Aspect
@Configuration
@Slf4j
public class LockMethodInterceptor {
    @Autowired
    private CacheService cacheService;

    /**保存方法拦截 10秒内不允许重复提交**/
    @Pointcut("execution(public * *(..)) && @annotation(com.dnt.data.standard.server.annotation.CacheLock)")
    public void saveIn10Seconds(){
        if(log.isInfoEnabled()) {
            log.info("LockMethodInterceptor-->saveIn5Seconds 5秒后保存切点");
        }
    }
    /**前置通知**/
    @Before( "saveIn10Seconds()")
    public void doInterceptorSave5Seconds(JoinPoint jp){
        if(log.isInfoEnabled()) {
            log.info("LockMethodInterceptor.doInterceptorSave5Seconds 切面 前置通知");
        }

        MethodSignature signature = (MethodSignature) jp.getSignature();
        Method method = signature.getMethod();
        CacheLock lock = method.getAnnotation(CacheLock.class);
        /**获取方法上的注解信息做为key**/
        String key = lock.prefix();
        /**10 秒内重复保存会报错误提示**/
        Boolean bLock = cacheService.lock("redisLock",key,key+" model lock",5);
        log.info(method.getName());
        if(!bLock){
            throw new ServiceException("5秒内不允许重复提交");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值