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

北京做网站比较有名的公司有哪些企业培训体系搭建

北京做网站比较有名的公司有哪些,企业培训体系搭建,apache 配置php网站,看房地产的app在哪看1、手写 Redis 分布式锁,包括上锁、解锁、自动续期。 此功能实现采用 Lua脚本实现,Lua脚本可以保证原子性。 setnx可以实现分布式锁,但是无法实现可重入锁,所以用hset来代替setnx实现可重入的分布式锁。 -- lock if redis.call…

1、手写 Redis 分布式锁,包括上锁、解锁、自动续期。

此功能实现采用 Lua脚本实现,Lua脚本可以保证原子性。

setnx可以实现分布式锁,但是无法实现可重入锁,所以用hset来代替setnx实现可重入的分布式锁。

-- lock
if redis.call('exists',KEYS[1]) == 0 or redis.call('hexists',KEYS[1],ARGV[1]) == 1 thenredis.call('hincrby',KEYS[1],ARGV[1],1)redis.call('expire',KEYS[1],ARGV[2])return 1
elsereturn 0
end
-- unlock
if redis.call('hexists',KEYS[1],ARGV[1]) == 0 thenreturn nil
elseif redis.call('hincrby',KEYS[1],ARGV[1],-1) == 0 thenreturn redis.call('del',KEYS[1])
elsereturn 0
end
-- expire
if redis.call('hexists',KEYS[1],ARGV[1]) == 0 thenreturn redis.call('expire',KEYS[1],ARGV[2])
elsereturn 0
end

2、工具类如下:

/*** @author xxx* @descpription: 自定义redis分布式锁* @date 2024/7/24*/
public class MyRedissonLua implements Lock {/***  加锁脚本*/private static final String lockScript ="if redis.call('exists',KEYS[1]) == 0 or redis.call('hexists',KEYS[1],ARGV[1]) == 1 then " +"redis.call('hincrby',KEYS[1],ARGV[1],1)    " +"redis.call('expire',KEYS[1],ARGV[2])    " +"return 1 " +"else    " +"return 0 " +"end";/*** 解锁脚本*/private static final String unLockScript ="if redis.call('HEXISTS',KEYS[1],ARGV[1]) == 0 then    " +"return nil " +"elseif redis.call('HINCRBY',KEYS[1],ARGV[1],-1) == 0 then    " +"return redis.call('del',KEYS[1]) " +"else    " +"return 0 " +"end";/***  续期脚本*/private static final String expireScript ="if redis.call('HEXISTS',KEYS[1],ARGV[1]) == 0 then  " +"    return redis.call('EXPIRE',KEYS[1],ARGV[2])     " +"else " +"    return 0 " +"end";private StringRedisTemplate stringRedisTemplate;/*** KEYS[1]*/private String lockName;/*** ARGV[1]*/private String uuidValue;/*** ARGV[2]*/private Long expireTime;public MyRedissonLua(StringRedisTemplate stringRedisTemplate, String lockName,String uuid) {this.stringRedisTemplate = stringRedisTemplate;this.lockName = lockName;this.uuidValue = uuid + ":" + Thread.currentThread().getId();this.expireTime = 30L;}@Overridepublic void lock() {tryLock();}@Overridepublic void unlock() {Long flag = stringRedisTemplate.execute(new DefaultRedisScript<>(unLockScript, Long.class), Arrays.asList(lockName), uuidValue);System.out.println("unlock lockName:" + lockName + "\tuuidValue:" + uuidValue + "\t expireTime:" + expireTime);if(flag == null) {throw new IllegalMonitorStateException("释放锁异常");}else {System.out.println("释放锁成功");}}@Overridepublic boolean tryLock() {boolean result;try {result = tryLock(-1L, TimeUnit.SECONDS);} catch (InterruptedException e) {throw new RuntimeException(e);}return result;}@Overridepublic boolean tryLock(long time, TimeUnit unit) throws InterruptedException {if (time == -1L){System.out.println("lockName:" + lockName + "\tuuidValue:" + uuidValue + "\t expireTime:" + expireTime);//可重入while (!stringRedisTemplate.execute(new DefaultRedisScript<>(lockScript, Boolean.class), Arrays.asList(lockName), uuidValue, String.valueOf(expireTime))){TimeUnit.MILLISECONDS.sleep(60);}//后台扫描程序,检测key的ttl,来实现续期reExpire();return true;}return false;}private void reExpire() {//每 10s 续期一次new Timer().schedule(new TimerTask(){@Overridepublic void run() {System.out.println(Thread.currentThread().getName() + " 续期");if (stringRedisTemplate.execute(new DefaultRedisScript<>(lockScript, Boolean.class), Arrays.asList(lockName), uuidValue, String.valueOf(expireTime))){reExpire();}}},(this.expireTime * 1000) / 3);}@Overridepublic void lockInterruptibly() {}@Overridepublic Condition newCondition() {return null;}
}

3、由于实现分布式锁的方式有很多,故采用工厂模式

/*** @author xxx* @descpription: 工厂模式生产分布式锁* @date 2024/7/24*/
@Component
public class DistributedLockFactory {private String lockName;private String uuid;@Resourceprivate StringRedisTemplate stringRedisTemplate;public DistributedLockFactory() {this.uuid = IdUtil.simpleUUID();}public Lock getDistributedLock(String lockType){if (lockType == null) {return null;}if (lockType.equals("REDIS")){lockName = "zzyyRedisLock";return new MyRedissonLua(stringRedisTemplate, lockName,uuid);} else if (lockType.equals("ZOOKEEPER")) {lockName = "zzyyZookeeperLock";//...Zookeeper版本的分布式锁return null;}return null;}
}

4、业务代码

import cn.hutool.core.util.IdUtil;
import com.coco.service.ICardService;
import com.coco.utils.lua.DistributedLockFactory;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;/*** @author xxx* @descpription: * @date 2024/7/18*/
@Service
public class ICardServiceImpl implements ICardService {@Resourceprivate StringRedisTemplate stringRedisTemplate;private static final String KEY = "sale001";@Value("${server.port}")private String port;@Resourceprivate RedissonClient redissonClient;/*** 自定义的redis分布式锁*/
//    private Lock lock = new MyRedissonLua(stringRedisTemplate, "zzyyRedisLock");/*** 通过工厂获取自定义的redis分布式锁*/@Resourceprivate DistributedLockFactory distributedLockFactory;@Overridepublic String sale() {version7();return "success";}private void version7() {Lock lock = distributedLockFactory.getDistributedLock("REDIS");lock.lock();try{String countStr = stringRedisTemplate.opsForValue().get(KEY);Integer count = countStr == null ? 0 : Integer.parseInt(countStr);if (count > 0) {stringRedisTemplate.opsForValue().set(KEY,String.valueOf(--count));System.out.println("剩余库存为:" + stringRedisTemplate.opsForValue().get(KEY) + "服务端口:" + port);//演示自动续期try {TimeUnit.SECONDS.sleep(120);} catch (InterruptedException e) {throw new RuntimeException(e);}}else {System.out.println("没有库存了");}}finally {lock.unlock();}}/*** 可重入锁*/private void version6() {Lock lock = distributedLockFactory.getDistributedLock("REDIS");lock.lock();try{String countStr = stringRedisTemplate.opsForValue().get(KEY);Integer count = countStr == null ? 0 : Integer.parseInt(countStr);if (count > 0) {stringRedisTemplate.opsForValue().set(KEY,String.valueOf(--count));System.out.println("剩余库存为:" + stringRedisTemplate.opsForValue().get(KEY) + "服务端口:" + port);//可重入testReEntry();}else {System.out.println("没有库存了");}}finally {lock.unlock();}}/***  可重入锁*/private void testReEntry() {Lock lock = distributedLockFactory.getDistributedLock("REDIS");lock.lock();try {System.out.println("===========再次获取锁=============");} finally {lock.unlock();}}/*** 通过Lua脚本实现分布式锁解锁*/private void version5() {String key = "zzyyRedisLock";String uuidValue = IdUtil.simpleUUID() +":" + Thread.currentThread().getId();//分布式锁(自旋)while (!stringRedisTemplate.opsForValue().setIfAbsent(key, uuidValue,10,TimeUnit.SECONDS)) {try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}}try {String countStr = stringRedisTemplate.opsForValue().get(KEY);Integer count = countStr == null ? 0 : Integer.parseInt(countStr);if (count > 0) {stringRedisTemplate.opsForValue().set(KEY,String.valueOf(--count));System.out.println("剩余库存为:" + stringRedisTemplate.opsForValue().get(KEY) + "服务端口:" + port);}else {System.out.println("没有库存了");}} finally {String script ="if redis.call(\"get\",KEYS[1]) == ARGV[1] then\n" +"    return redis.call(\"del\",KEYS[1])\n" +"else\n" +"    return 0\n" +"end";stringRedisTemplate.execute(new DefaultRedisScript<>(script, Boolean.class),Arrays.asList(key), uuidValue);}}/*** 添加判断防止解锁解的不是同一把锁*/private void version4() {String key = "zzyyRedisLock";String uuidValue = IdUtil.simpleUUID()+":" + Thread.currentThread().getId();//分布式锁(自旋)while (!stringRedisTemplate.opsForValue().setIfAbsent(key, uuidValue,10,TimeUnit.SECONDS)) {try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}}try {String countStr = stringRedisTemplate.opsForValue().get(KEY);Integer count = countStr == null ? 0 : Integer.parseInt(countStr);if (count > 0) {stringRedisTemplate.opsForValue().set(KEY,String.valueOf(--count));System.out.println("剩余库存为:" + stringRedisTemplate.opsForValue().get(KEY) + "服务端口:" + port);}else {System.out.println("没有库存了");}} finally {if (Objects.equals(uuidValue, stringRedisTemplate.opsForValue().get(key))){stringRedisTemplate.delete(key);}}}/*** 通过setnx实现redis分布式锁*/private void version3() {String key = "zzyyRedisLock";String uuidValue = IdUtil.simpleUUID()+":" + Thread.currentThread().getId();//分布式锁Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, uuidValue);if (flag) {try {String countStr = stringRedisTemplate.opsForValue().get(KEY);Integer count = countStr == null ? 0 : Integer.parseInt(countStr);if (count > 0) {stringRedisTemplate.opsForValue().set(KEY,String.valueOf(--count));System.out.println("剩余库存为:" + stringRedisTemplate.opsForValue().get(KEY) + "服务端口:" + port);}else {System.out.println("没有库存了");}} finally {if (Objects.equals(uuidValue, stringRedisTemplate.opsForValue().get(key))){stringRedisTemplate.delete(key);}}}else {try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}sale();}}/*** juc的lock锁*/private void version2() {
//        lock.lock();
//        try {
//            Integer count = (Integer) redisTemplate.opsForValue().get(KEY);
//            if (count > 0) {
//                redisTemplate.opsForValue().decrement(KEY);
//                System.out.println("剩余库存为:" + redisTemplate.opsForValue().get(KEY) + "服务端口:" + port);
//            }else {
//                System.out.println("没有库存了");
//            }
//        } finally {
//            lock.unlock();
//        }}
}

http://www.fp688.cn/news/160730.html

相关文章:

  • 网站建设一般好久到期怎样在百度上做广告推广
  • 网站搭建后台宁波关键词优化企业网站建设
  • 做网站点击软件百度关键词优化点击 教程
  • 赣州建网站做网站怎么赚钱
  • No酒类网站建设seo培训价格
  • 企业网站建设联系电话网络推广宣传方式
  • 工程建设造价全过程监督网站快速优化网站排名软件
  • 台州网站建设方案优化河北百度推广客服电话
  • 嘉定区整站seo十大排名热搜榜排名今日事件
  • asp做网站的步骤中国最好的营销策划公司
  • 在家做的客服正规平台靠谱seo外包定制
  • 婚纱摄影网站开题报告网站推广的主要方法
  • 网站cms相关知识完整的网页设计代码
  • 自己怎么做微信小程序免费武汉seo服务外包
  • 网站建设衤金手指花总天津seo建站
  • 有合作做时时彩网站的吗想卖产品怎么推广宣传
  • 南京建站公司网站企业培训的目的和意义
  • 怎么样增加网站权重hyein seo
  • 口碑好的免费网站建设链接交换公司
  • 青岛知名网站建设哪家好成人电脑基础培训班
  • 网站建设 公司 广州济南网站优化公司哪家好
  • 中国十大高端设计公司一键关键词优化
  • 中铁雄安建设有限公司网站网站建设及网络推广
  • wordpress 媒体库分类北京seo外包平台
  • 高站网站建设推广普通话演讲稿
  • 网站怎么做推广知乎seo关键词排名优化制作
  • WordPress标题换行显示网站如何优化排名
  • 网站做背景不显示谷歌查询关键词的工具叫什么
  • 从化营销网站建设现在最火的发帖平台
  • 优化网站制作百度咨询电话 人工