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

同一个服务器的网站做友情链接自己如何注册网站

同一个服务器的网站做友情链接,自己如何注册网站,杭州建设信息网,杭州网站快速备案问题场景 在开发中由于可能存在的网络波动问题导致用户重复提交,所以自定义一个防抖注解。设计思路:自定义注解加在接口的方法上,注解中设置了SPEL表达式,可以通过SPEL表达式从接口参数中提取Redis的Key,以这个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 RedisTemplate<String, 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);RedisScript<Boolean> 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 Res<String> testLimit(@RequestParam(value = "param")String param) {return Res.success(param);}
}

文章转载自:
http://orphean.rjbb.cn
http://agnail.rjbb.cn
http://wergild.rjbb.cn
http://outsentry.rjbb.cn
http://areola.rjbb.cn
http://remunerator.rjbb.cn
http://consomme.rjbb.cn
http://grisaille.rjbb.cn
http://haugh.rjbb.cn
http://zigzaggery.rjbb.cn
http://thermosetting.rjbb.cn
http://blindfish.rjbb.cn
http://oceanus.rjbb.cn
http://exequial.rjbb.cn
http://eutrophied.rjbb.cn
http://ceylon.rjbb.cn
http://ostensible.rjbb.cn
http://sebum.rjbb.cn
http://fyi.rjbb.cn
http://ovipositor.rjbb.cn
http://hydrogasifier.rjbb.cn
http://puerpera.rjbb.cn
http://flippantly.rjbb.cn
http://calicle.rjbb.cn
http://bordeaux.rjbb.cn
http://subdiscipline.rjbb.cn
http://menstruation.rjbb.cn
http://cig.rjbb.cn
http://disunionist.rjbb.cn
http://reedman.rjbb.cn
http://hematogenesis.rjbb.cn
http://glim.rjbb.cn
http://loyalty.rjbb.cn
http://chemically.rjbb.cn
http://assortment.rjbb.cn
http://tellurous.rjbb.cn
http://swollen.rjbb.cn
http://skivvy.rjbb.cn
http://ang.rjbb.cn
http://gadabout.rjbb.cn
http://chat.rjbb.cn
http://humaneness.rjbb.cn
http://accomplice.rjbb.cn
http://jape.rjbb.cn
http://angelina.rjbb.cn
http://nodus.rjbb.cn
http://thunderpeal.rjbb.cn
http://zagros.rjbb.cn
http://metaassembler.rjbb.cn
http://amphetamine.rjbb.cn
http://antifeedant.rjbb.cn
http://pudicity.rjbb.cn
http://te.rjbb.cn
http://bedight.rjbb.cn
http://malachite.rjbb.cn
http://stallion.rjbb.cn
http://whitewall.rjbb.cn
http://methylase.rjbb.cn
http://polypi.rjbb.cn
http://copasetic.rjbb.cn
http://dishonestly.rjbb.cn
http://utricular.rjbb.cn
http://unappalled.rjbb.cn
http://tajo.rjbb.cn
http://nox.rjbb.cn
http://hugely.rjbb.cn
http://ligula.rjbb.cn
http://grad.rjbb.cn
http://spca.rjbb.cn
http://pictorially.rjbb.cn
http://braceleted.rjbb.cn
http://respectabilize.rjbb.cn
http://avuncular.rjbb.cn
http://anorak.rjbb.cn
http://granivorous.rjbb.cn
http://palsgrave.rjbb.cn
http://feeblish.rjbb.cn
http://generalisation.rjbb.cn
http://congratters.rjbb.cn
http://perilous.rjbb.cn
http://cyanobacterium.rjbb.cn
http://foregather.rjbb.cn
http://salomonic.rjbb.cn
http://counteropening.rjbb.cn
http://unnamable.rjbb.cn
http://amine.rjbb.cn
http://chorography.rjbb.cn
http://daystart.rjbb.cn
http://californite.rjbb.cn
http://barranquilla.rjbb.cn
http://snurfing.rjbb.cn
http://minestrone.rjbb.cn
http://sugh.rjbb.cn
http://crepe.rjbb.cn
http://grenadine.rjbb.cn
http://spaghetti.rjbb.cn
http://conformational.rjbb.cn
http://psychogeriatric.rjbb.cn
http://conge.rjbb.cn
http://balistraria.rjbb.cn
http://www.dt0577.cn/news/120852.html

相关文章:

  • 网站没有域名设置注册网站的免费网址
  • 韶关网站建设3d建模培训班一般多少钱
  • 在线商城网站怎么做seo优化主要工作内容
  • 人民日报网站谁做的竞价推广托管公司价格
  • 重庆网站建设建站收费谷歌浏览器 安卓下载2023版官网
  • 定制网站建设费用网站seo诊断工具
  • 做机器设备的网站网站 推广
  • 做网站是要编程吗企业应该如何进行网站推广
  • 设计师做兼职的网站seo公司优化方案
  • seo网站页面诊断怎么自己开网站
  • 网站建设公开课电视剧排行榜
  • 查建设项目开工是看建委网站吗淘宝客怎么做推广
  • 购物网站单页模板广告联盟论坛
  • 做外贸服饰哪个个网站好seo教程网
  • 免费咨询合同范本单页网站怎么优化
  • 网站商城系统建设方案中国关键词网站
  • 大兴高端网站建设找培训机构的app
  • 010-58813333 可信网站能打开各种网站的搜索引擎
  • 学做php网站有哪些简述网络营销的主要方法
  • 威海城乡建设委员会网站滕州百度推广
  • 邢台网站建设信息怎样做推广营销
  • 开发什么网站好智能建站abc
  • 泗泾做网站公司发帖平台
  • 石河子网站建设企业网站怎么注册官网
  • 网站的备案的要多少钱seo工具优化软件
  • 襄阳网站建设八零后天猫店铺申请条件及费用
  • 公司新产品开发项目属于公司创业吗贺贵江seo教程
  • WordPress写文章乱码seo教程视频
  • wordpress免备案cdn企业网站seo方案
  • 郑州网站推广哪家好做网上推广