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

Usage of redis distributed Lock

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the usage of redis distributed lock". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of redis distributed lock".

Redis distributed lock

There is no technical content, do not want to write directly copy it, right?

Based on SpringDataRedis

Import org.apache.commons.lang3.StringUtils;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;import javax.annotation.Resource;import java.time.Duration;import java.util.concurrent.TimeUnit;/** * @ author: xuchonggao * @ since: 2019-10-11 * / @ Componentpublic class RedisLock {@ Resource StringRedisTemplate stringRedisTemplate Private RedisLock () {} / * redis lock * * @ param key the key * @ param waitTime to be locked gets the waiting time blocking time of the lock * @ param expireTime lock expiration time defaults to 10s * @ return * / public boolean lock (String key, Duration waitTime, Duration expireTime) {if (StringUtils.isBlank (key)) {return false } long start = System.currentTimeMillis (); long waitTimeMillis = waitTime! = null? WaitTime.toMillis (): 0L; if (expireTime = = null) {expireTime = Duration.ofSeconds (10);} do {Boolean lockRes = stringRedisTemplate.opsForValue (). SetIfAbsent (key, StringUtils.EMPTY, expireTime); if (Boolean.TRUE.equals (lockRes)) {return true } try {TimeUnit.MILLISECONDS.sleep;} catch (InterruptedException ignore) {}} while (System.currentTimeMillis ()-start < waitTimeMillis); return false } / * redis lock * non-blocking * @ param key key * @ param expireTime lock expiration time * @ return * / public boolean lock (String key, Duration expireTime) {return lock (key, null, expireTime) } / * redis lock * default 10 seconds expired non-blocking * * @ param key key * @ return * / public boolean lock (String key) {return lock (key, null, null) } / * * unlock * * @ param key * / public void unlock (String key) {stringRedisTemplate.delete (key) }} Thank you for your reading. this is the content of "the usage of redis distributed Lock". After the study of this article, I believe you have a deeper understanding of the usage of redis distributed lock, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report