当前位置: 首页 > news >正文

滁州58同城网站怎么做编程猫官网

滁州58同城网站怎么做,编程猫官网,创意极简logo,wordpress投稿vip问题场景 在开发中由于可能存在的网络波动问题导致用户重复提交#xff0c;所以自定义一个防抖注解。设计思路#xff1a;自定义注解加在接口的方法上#xff0c;注解中设置了SPEL表达式#xff0c;可以通过SPEL表达式从接口参数中提取Redis的Key#xff0c;以这个Key作为…问题场景 在开发中由于可能存在的网络波动问题导致用户重复提交所以自定义一个防抖注解。设计思路自定义注解加在接口的方法上注解中设置了SPEL表达式可以通过SPEL表达式从接口参数中提取Redis的Key以这个Key作为判断是否重复提交的依据。如果没有设置SPEL表达式的话就以当前登录用户的ID作为Key。同时在将数据设置到缓存的时候使用Lua脚本执行保证Redis命令的原子性。 代码实现 自定义注解 package com.creatar.common.annotation;import java.lang.annotation.*;/*** 防抖注解** author: 张定辉* date: 2024/6/13 上午9:43* description: 防抖注解*/ Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Documented public interface RepeatLock {/*** SPEL表达式,根据该表达式解析出的值作为key** return SPEL表达式解析得到的值*/String value();/*** redis前缀*/String prefix() default repeat_lock::;/*** 错误提示信息*/String message() default 请勿重复提交!;/*** 设置单位时间内禁止重复提交以秒为单位*/int unitTime() default 3; }AOP注解处理器 package com.creatar.common.annotation.handler;import com.creatar.common.annotation.RepeatLock; import com.creatar.exception.CustomException; import com.creatar.util.SecurityUtil; import com.creatar.util.SpelUtil; import lombok.RequiredArgsConstructor; 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.reflect.MethodSignature; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; import org.springframework.data.redis.core.script.RedisScript; import org.springframework.expression.EvaluationContext; import org.springframework.stereotype.Component;import java.util.Collections; import java.util.Objects;/*** 防抖注解处理器** author: 张定辉* date: 2024/6/13 上午9:49* description: 防抖注解处理器*/ Aspect RequiredArgsConstructor Slf4j Component public class RepeatLockAspect {private final RedisTemplateString, String redisTemplate;Before(annotation(repeatLock))public void before(JoinPoint joinPoint, RepeatLock repeatLock) {String redisPrefix repeatLock.prefix();String errorMessage repeatLock.message();String value repeatLock.value();int unitTime repeatLock.unitTime();MethodSignature signature (MethodSignature) joinPoint.getSignature();EvaluationContext context SpelUtil.getContext(joinPoint.getArgs(), signature.getMethod());String key SecurityUtil.getCurrentUserId();try {key key SpelUtil.getValue(context, value, String.class);} catch (Exception e) {log.error(防抖注解获取SPEL表达式失败,类名称:{},方法名称{}\n, joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), e);}String redisKey redisPrefix key;//如果是重复提交则抛出异常if (isRepeat(redisKey,unitTime)) {throw new CustomException(errorMessage);}}/*** 使用Lua脚本执行原子性的Redis操作避免由于并发过大从而导致的key永久有效* 如果key不存在则设置value为1并且设置过期时间** return 如果没有key则false如果有key则返回true表示重复提交*/private boolean isRepeat(String key,int unitTime) {String scriptStr if redis.call(exists, KEYS[1]) 0 thenredis.call(set, KEYS[1], 1, ex,%s)return falseelsereturn trueend.formatted(unitTime);RedisScriptBoolean script new DefaultRedisScript(scriptStr, Boolean.class);Boolean result redisTemplate.execute(script, Collections.singletonList(key));if (Objects.isNull(result)) {return true;}return result;} }应用 package com.creatar.controller;import com.creatar.common.Res; import com.creatar.common.annotation.RepeatLock; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.security.PermitAll; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;/*** 测试接口** author: 张定辉* date: 2024/6/15 下午4:36* description: 测试接口*/ RestController RequestMapping(/test) Tag(name 测试接口,description 测试接口) PermitAll public class TestController {GetMapping(/testLimit)RepeatLock(value #param,unitTime 5)public ResString testLimit(RequestParam(value param)String param) {return Res.success(param);} }
http://www.hkea.cn/news/14442526/

相关文章:

  • 如何自己做游戏软件北京seo课程培训
  • 网站备案主办单位性质村级网站建设 不断增强
  • 网站建设技术方面论文wordpress图片0x0
  • 高端品牌网站建设兴田德润怎么联系东莞建筑
  • 如何自建网站?品牌网站和优化网站
  • html5网站模板舒城县建设局网站
  • icp备案网站财务网站建设
  • 建网站需要注意什么抖音seo培训
  • 网站建设推广岗位成都微信小程序推广培训公司
  • 做网站人员有什么名称wordpress自动生成密码
  • 网站建设售后sae wordpress 更新
  • 熵网站企业网站域名注册
  • 网站备案号查询系统做科研交流常用的网站
  • 建设厅网站的秘钥怎么买百度搜索关键词优化
  • 网站加载很慢怎样与其它网站做友情链接
  • 湖南基础建设投资集团网站有的网站在浏览器打不开怎么办
  • 福建嘉瑞建设工程有限公司网站怀化网络推广哪家服务好
  • 根河企业网站建设郑州app制作一个需要多少钱
  • 手机软件网站凡科做网站不好
  • 做mla的网站工作网站开发制作
  • 长春餐饮网站建设中铁建设集团门户网站登陆
  • 电子商务网站建设考卷数字营销招聘
  • h5网站建设贵州seo技术查询
  • 网站中文域名好不好wordpress和drupal
  • 东阳网站推广wordpress wp_query
  • 池州网站建设开发什么是网络设计制作
  • 2018年静安区品牌网站建设优秀网站网页设计
  • 免费做优化的网站百度怎么自己做网站
  • 怎么做pdf电子书下载网站怎样运营推广网站
  • 银行网站开发技术方案盘锦网站建设咨询