您的位置:首页 > 教育 > 锐评 > 生活中的网页设计作品_美国疫情最新消息今天又封了_云南最新消息_重庆seo全网营销

生活中的网页设计作品_美国疫情最新消息今天又封了_云南最新消息_重庆seo全网营销

2025/4/20 23:16:28 来源:https://blog.csdn.net/weixin_43076660/article/details/145597058  浏览:    关键词:生活中的网页设计作品_美国疫情最新消息今天又封了_云南最新消息_重庆seo全网营销
生活中的网页设计作品_美国疫情最新消息今天又封了_云南最新消息_重庆seo全网营销
 <dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.26.0</version></dependency>
import org.redisson.api.*;import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;/*** Redis 服务** @author Fuzhengwei bugstack.cn @小傅哥*/
public interface IRedisService {/*** 设置指定 key 的值** @param key   键* @param value 值*/<T> void setValue(String key, T value);/*** 设置指定 key 的值** @param key     键* @param value   值* @param expired 过期时间*/<T> void setValue(String key, T value, long expired);/*** 获取指定 key 的值** @param key 键* @return 值*/<T> T getValue(String key);/*** 获取队列** @param key 键* @param <T> 泛型* @return 队列*/<T> RQueue<T> getQueue(String key);/*** 加锁队列** @param key 键* @param <T> 泛型* @return 队列*/<T> RBlockingQueue<T> getBlockingQueue(String key);/*** 延迟队列** @param rBlockingQueue 加锁队列* @param <T>            泛型* @return 队列*/<T> RDelayedQueue<T> getDelayedQueue(RBlockingQueue<T> rBlockingQueue);/*** 设置值** @param key   key 键* @param value 值*/void setAtomicLong(String key, long value);/*** 获取值** @param key key 键*/Long getAtomicLong(String key);/*** 自增 Key 的值;1、2、3、4** @param key 键* @return 自增后的值*/long incr(String key);/*** 指定值,自增 Key 的值;1、2、3、4** @param key 键* @return 自增后的值*/long incrBy(String key, long delta);/*** 自减 Key 的值;1、2、3、4** @param key 键* @return 自增后的值*/long decr(String key);/*** 指定值,自增 Key 的值;1、2、3、4** @param key 键* @return 自增后的值*/long decrBy(String key, long delta);/*** 移除指定 key 的值** @param key 键*/void remove(String key);/*** 判断指定 key 的值是否存在** @param key 键* @return true/false*/boolean isExists(String key);/*** 将指定的值添加到集合中** @param key   键* @param value 值*/void addToSet(String key, String value);/*** 判断指定的值是否是集合的成员** @param key   键* @param value 值* @return 如果是集合的成员返回 true,否则返回 false*/boolean isSetMember(String key, String value);/*** 将指定的值添加到列表中** @param key   键* @param value 值*/void addToList(String key, String value);/*** 获取列表中指定索引的值** @param key   键* @param index 索引* @return 值*/String getFromList(String key, int index);/*** 获取Map** @param key 键* @return 值*/<K, V> RMap<K, V> getMap(String key);/*** 将指定的键值对添加到哈希表中** @param key   键* @param field 字段* @param value 值*/void addToMap(String key, String field, String value);/*** 获取哈希表中指定字段的值** @param key   键* @param field 字段* @return 值*/String getFromMap(String key, String field);/*** 获取哈希表中指定字段的值** @param key   键* @param field 字段* @return 值*/<K, V> V getFromMap(String key, K field);/*** 将指定的值添加到有序集合中** @param key   键* @param value 值*/void addToSortedSet(String key, String value);/*** 获取 Redis 锁(可重入锁)** @param key 键* @return Lock*/RLock getLock(String key);/*** 获取 Redis 锁(公平锁)** @param key 键* @return Lock*/RLock getFairLock(String key);/*** 获取 Redis 锁(读写锁)** @param key 键* @return RReadWriteLock*/RReadWriteLock getReadWriteLock(String key);/*** 获取 Redis 信号量** @param key 键* @return RSemaphore*/RSemaphore getSemaphore(String key);/*** 获取 Redis 过期信号量* <p>* 基于Redis的Redisson的分布式信号量(Semaphore)Java对象RSemaphore采用了与java.util.concurrent.Semaphore相似的接口和用法。* 同时还提供了异步(Async)、反射式(Reactive)和RxJava2标准的接口。** @param key 键* @return RPermitExpirableSemaphore*/RPermitExpirableSemaphore getPermitExpirableSemaphore(String key);/*** 闭锁** @param key 键* @return RCountDownLatch*/RCountDownLatch getCountDownLatch(String key);/*** 布隆过滤器** @param key 键* @param <T> 存放对象* @return 返回结果*/<T> RBloomFilter<T> getBloomFilter(String key);Boolean setNx(String key);Boolean setNx(String key, long expired, TimeUnit timeUnit);RBitSet getBitSet(String key);default int getIndexFromUserId(String userId) {try {MessageDigest md = MessageDigest.getInstance("MD5");byte[] hashBytes = md.digest(userId.getBytes(StandardCharsets.UTF_8));// 将哈希字节数组转换为正整数BigInteger bigInt = new BigInteger(1, hashBytes);// 取模以确保索引在合理范围内return bigInt.mod(BigInteger.valueOf(Integer.MAX_VALUE)).intValue();} catch (NoSuchAlgorithmException e) {throw new RuntimeException("MD5 algorithm not found", e);}}}
import org.redisson.api.*;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.time.Duration;
import java.util.concurrent.TimeUnit;/*** Redis 服务 - Redisson** @author Fuzhengwei bugstack.cn @小傅哥*/
@Service("redissonService")
public class RedissonService implements IRedisService {@Resourceprivate RedissonClient redissonClient;public <T> void setValue(String key, T value) {redissonClient.<T>getBucket(key).set(value);}@Overridepublic <T> void setValue(String key, T value, long expired) {RBucket<T> bucket = redissonClient.getBucket(key);bucket.set(value, Duration.ofMillis(expired));}public <T> T getValue(String key) {return redissonClient.<T>getBucket(key).get();}@Overridepublic <T> RQueue<T> getQueue(String key) {return redissonClient.getQueue(key);}@Overridepublic <T> RBlockingQueue<T> getBlockingQueue(String key) {return redissonClient.getBlockingQueue(key);}@Overridepublic <T> RDelayedQueue<T> getDelayedQueue(RBlockingQueue<T> rBlockingQueue) {return redissonClient.getDelayedQueue(rBlockingQueue);}@Overridepublic void setAtomicLong(String key, long value) {redissonClient.getAtomicLong(key).set(value);}@Overridepublic Long getAtomicLong(String key) {return redissonClient.getAtomicLong(key).get();}@Overridepublic long incr(String key) {return redissonClient.getAtomicLong(key).incrementAndGet();}@Overridepublic long incrBy(String key, long delta) {return redissonClient.getAtomicLong(key).addAndGet(delta);}@Overridepublic long decr(String key) {return redissonClient.getAtomicLong(key).decrementAndGet();}@Overridepublic long decrBy(String key, long delta) {return redissonClient.getAtomicLong(key).addAndGet(-delta);}@Overridepublic void remove(String key) {redissonClient.getBucket(key).delete();}@Overridepublic boolean isExists(String key) {return redissonClient.getBucket(key).isExists();}public void addToSet(String key, String value) {RSet<String> set = redissonClient.getSet(key);set.add(value);}public boolean isSetMember(String key, String value) {RSet<String> set = redissonClient.getSet(key);return set.contains(value);}public void addToList(String key, String value) {RList<String> list = redissonClient.getList(key);list.add(value);}public String getFromList(String key, int index) {RList<String> list = redissonClient.getList(key);return list.get(index);}@Overridepublic <K, V> RMap<K, V> getMap(String key) {return redissonClient.getMap(key);}public void addToMap(String key, String field, String value) {RMap<String, String> map = redissonClient.getMap(key);map.put(field, value);}public String getFromMap(String key, String field) {RMap<String, String> map = redissonClient.getMap(key);return map.get(field);}@Overridepublic <K, V> V getFromMap(String key, K field) {return redissonClient.<K, V>getMap(key).get(field);}public void addToSortedSet(String key, String value) {RSortedSet<String> sortedSet = redissonClient.getSortedSet(key);sortedSet.add(value);}@Overridepublic RLock getLock(String key) {return redissonClient.getLock(key);}@Overridepublic RLock getFairLock(String key) {return redissonClient.getFairLock(key);}@Overridepublic RReadWriteLock getReadWriteLock(String key) {return redissonClient.getReadWriteLock(key);}@Overridepublic RSemaphore getSemaphore(String key) {return redissonClient.getSemaphore(key);}@Overridepublic RPermitExpirableSemaphore getPermitExpirableSemaphore(String key) {return redissonClient.getPermitExpirableSemaphore(key);}@Overridepublic RCountDownLatch getCountDownLatch(String key) {return redissonClient.getCountDownLatch(key);}@Overridepublic <T> RBloomFilter<T> getBloomFilter(String key) {return redissonClient.getBloomFilter(key);}@Overridepublic Boolean setNx(String key) {return redissonClient.getBucket(key).trySet("lock");}@Overridepublic Boolean setNx(String key, long expired, TimeUnit timeUnit) {return redissonClient.getBucket(key).trySet("lock", expired, timeUnit);}@Overridepublic RBitSet getBitSet(String key) {return redissonClient.getBitSet(key);}}

采用getLock方式

 @Resourceprivate IRedisService redisService;@Overridepublic String groupBuyNotify(NotifyTaskEntity notifyTask) throws Exception {RLock lock = redisService.getLock(notifyTask.lockKey());try {// 服务端会被部署到多台应用服务器上,那么就会有很多任务一起执行。这个时候要进行抢占,避免被多次执行if (lock.tryLock(3, 0, TimeUnit.SECONDS)) {try {return "";} finally {if (lock.isLocked() && lock.isHeldByCurrentThread()) {lock.unlock();}}}return NotifyTaskHTTPEnumVO.NULL.getCode();} catch (Exception e) {Thread.currentThread().interrupt();return NotifyTaskHTTPEnumVO.NULL.getCode();}}

采用setNx方式

 String lockKey = cacheKey + Constants.UNDERLINE + surplus;Boolean lock = false;
long expireMillis = endDateTime.getTime() - System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1);
lock = redisService.setNx(lockKey, expireMillis, TimeUnit.MILLISECONDS);if (!lock) {log.info("加锁失败 {}", lockKey);}

原子性扣减

//创建if (redisService.isExists(cacheKey)) return;redisService.setAtomicLong(cacheKey, stockCount);//使用
long surplus = redisService.decr(cacheKey);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com