Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize springboot idempotent slicing

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize springboot idempotent slices". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

Recently, the test raised the problem of repeated submission of some APIs. After thinking about the problem that the button is not clickable after the front end is clicked, the back end should make a multi-layer judgment based on login token, operation method and parameters.

2. Examples

Slicing code

/ * * the idempotent section of the interface * @ author Administrator * / @ Slf4j@Aspect@Componentpublic class ApiIdempotentAspect {@ Resource RedisUtil redisUtil; @ Resource UserUtils userUtils @ Pointcut ("@ annotation (com.xx.anno.ApiIdempotent)") private void pointCut () {} @ Before ("pointCut ()") public void doPoint (JoinPoint joinPoint) {String action = joinPoint.getSignature () .getDeclaringTypeName () .substring (joinPoint.getSignature (). GetDeclaringTypeName (). LastIndexOf (".") + ":" + joinPoint.getSignature () .getName () String args = JSON.toJSONString (joinPoint.getArgs ()); String token = userUtils.getAuthToke () .replace ("-", "). Replace (" Bearer ","); String idempotentKey =" api::idempotent:: "+ token+":: "+ action / / similar operations have not been performed in a short period of time if (redisUtil.hasKey (idempotentKey)) {/ / whether the interface parameters are consistent String idempotentValue = redisUtil.getCacheObject (idempotentKey); log.info ("idempotentValue: {}", idempotentValue); if (args.equals (idempotentValue)) {throw new BusinessException ("do not repeat operations") }} else {/ / 30s prohibit repeated operation redisUtil.setCacheObject (idempotentKey,args,30, TimeUnit.SECONDS);}

Use a redisutil.

@ Componentpublic class RedisUtil {@ Resource public RedisTemplate redisTemplate; / * cache basic objects, key values of Integer, String, entity classes, etc. * @ param key cache * @ param value cache values * / public void setCacheObject (final String key, final T value) {redisTemplate.opsForValue () .set (key, value) } / * cache basic objects, key values of Integer, String, entity classes, etc. * @ param key cache * @ param value cache value * @ param timeout time * @ param timeUnit time granularity * / public void setCacheObject (final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {redisTemplate.opsForValue (). Set (key, value, timeout, timeUnit) } / * set valid time * * @ param key Redis key * @ param timeout timeout * @ return true= set successfully False= setting failed * / public boolean expire (final String key, final long timeout) {return expire (key, timeout, TimeUnit.SECONDS);} / * * set valid time * * @ param key Redis key * @ param timeout timeout * @ param unit time unit * @ return true= set successfully False= setting failed * / public boolean expire (final String key, final long timeout, final TimeUnit unit) {return redisTemplate.expire (key, timeout, unit);} / * get the cached basic object. * * @ param key cache key value * @ return cache key value corresponding to data * / public T getCacheObject (final String key) {ValueOperations operation = redisTemplate.opsForValue (); return operation.get (key);} / * * Delete a single object * * @ param key * / public boolean deleteObject (final String key) {return redisTemplate.delete (key) } / * * Delete collection objects * * @ param collection multiple objects * @ return * / public long deleteObject (final Collection collection) {return redisTemplate.delete (collection) } / * * cached List data * * @ param key cached key value * @ param dataList List data to be cached * @ return cached object * / public long setCacheList (final String key, final List dataList) {Long count = redisTemplate.opsForList () .rightPushAll (key, dataList); return count = = null? 0: count } / * get the cached list object * * @ param key cached key value * @ return cached key value corresponding to the data * / public List getCacheList (final String key) {return redisTemplate.opsForList () .range (key, 0,-1) } / * * cached Set * * @ param key cache key * @ param dataSet cached data * @ return cached data object * / public BoundSetOperations setCacheSet (final String key, final Set dataSet) {BoundSetOperations setOperation = redisTemplate.boundSetOps (key); Iterator it = dataSet.iterator () While (it.hasNext ()) {setOperation.add (it.next ());} return setOperation;} / * * get the cached set * * @ param key * @ return * / public Set getCacheSet (final String key) {return redisTemplate.opsForSet () .members (key) } / * cache Map * * @ param key * @ param dataMap * / public void setCacheMap (final String key, final Map dataMap) {if (dataMap! = null) {redisTemplate.opsForHash () .putAll (key, dataMap) }} / * get the cached Map * * @ param key * @ return * / public Map getCacheMap (final String key) {return redisTemplate.opsForHash () .entries (key) } / * to store data in Hash * * @ param key Redis key * @ param hKey Hash key * @ param value value * / public void setCacheMapValue (final String key, final String hKey, final T value) {redisTemplate.opsForHash () .put (key, hKey, value) } / * get data in Hash * * @ param key Redis key * @ param hKey Hash key * @ return Hash object * / public T getCacheMapValue (final String key, final String hKey) {HashOperations opsForHash = redisTemplate.opsForHash (); return opsForHash.get (key, hKey) } / * get data in multiple Hash * * @ param key Redis key * @ param hKeys Hash key collection * @ return Hash object collection * / public List getMultiCacheMapValue (final String key, final Collection hKeys) {return redisTemplate.opsForHash () .multiGet (key, hKeys) } / * get cached basic object list * * @ param pattern string prefix * @ return object list * / public Collection keys (final String pattern) {return redisTemplate.keys (pattern) } / * to determine whether key is still in * @ param key * @ return * / public boolean hasKey (final String key) {return redisTemplate.hasKey (key);}} "how to implement springboot idempotent slices" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report