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

北京做网站比较有名的公司有哪些手机网站seo免费软件

北京做网站比较有名的公司有哪些,手机网站seo免费软件,flask做视频网站,ubuntu 安装wordpress1、手写 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://graft.tyjp.cn
http://persorption.tyjp.cn
http://fluidize.tyjp.cn
http://kcmg.tyjp.cn
http://drygoods.tyjp.cn
http://coruscant.tyjp.cn
http://conge.tyjp.cn
http://apiece.tyjp.cn
http://tinnery.tyjp.cn
http://sublimate.tyjp.cn
http://netcropper.tyjp.cn
http://crisp.tyjp.cn
http://morphic.tyjp.cn
http://internal.tyjp.cn
http://crosswalk.tyjp.cn
http://hegari.tyjp.cn
http://tensegrity.tyjp.cn
http://enlargement.tyjp.cn
http://gallica.tyjp.cn
http://nardu.tyjp.cn
http://lose.tyjp.cn
http://versemonger.tyjp.cn
http://vocation.tyjp.cn
http://iowa.tyjp.cn
http://ferrocyanide.tyjp.cn
http://cyberpunk.tyjp.cn
http://operetta.tyjp.cn
http://capture.tyjp.cn
http://titer.tyjp.cn
http://cosponsor.tyjp.cn
http://genic.tyjp.cn
http://cooperativize.tyjp.cn
http://olid.tyjp.cn
http://brack.tyjp.cn
http://pepo.tyjp.cn
http://cataleptiform.tyjp.cn
http://oceanus.tyjp.cn
http://churr.tyjp.cn
http://outmatch.tyjp.cn
http://ventriculography.tyjp.cn
http://dower.tyjp.cn
http://fuzzball.tyjp.cn
http://bikini.tyjp.cn
http://illusiveness.tyjp.cn
http://majestical.tyjp.cn
http://tremblant.tyjp.cn
http://sadder.tyjp.cn
http://lalopathy.tyjp.cn
http://damsite.tyjp.cn
http://sweeny.tyjp.cn
http://jain.tyjp.cn
http://conspire.tyjp.cn
http://alterne.tyjp.cn
http://cyrtosis.tyjp.cn
http://crowbar.tyjp.cn
http://citlaltepetl.tyjp.cn
http://sanmartinite.tyjp.cn
http://fruitless.tyjp.cn
http://insomnia.tyjp.cn
http://sweeper.tyjp.cn
http://pump.tyjp.cn
http://wright.tyjp.cn
http://flask.tyjp.cn
http://citrange.tyjp.cn
http://languidly.tyjp.cn
http://vladimirite.tyjp.cn
http://sumptuously.tyjp.cn
http://lowestoft.tyjp.cn
http://illawarra.tyjp.cn
http://adiaphorist.tyjp.cn
http://gamogenesis.tyjp.cn
http://expressivity.tyjp.cn
http://fluoroplastic.tyjp.cn
http://stroke.tyjp.cn
http://outcaste.tyjp.cn
http://doxographer.tyjp.cn
http://carnival.tyjp.cn
http://brockage.tyjp.cn
http://vasodilation.tyjp.cn
http://reveler.tyjp.cn
http://burgle.tyjp.cn
http://calorific.tyjp.cn
http://wildcatter.tyjp.cn
http://falculate.tyjp.cn
http://polacolor.tyjp.cn
http://exploded.tyjp.cn
http://catrigged.tyjp.cn
http://practicably.tyjp.cn
http://embellishment.tyjp.cn
http://slugger.tyjp.cn
http://pidgin.tyjp.cn
http://adiaphorism.tyjp.cn
http://concentre.tyjp.cn
http://krilium.tyjp.cn
http://agrochemical.tyjp.cn
http://genuflector.tyjp.cn
http://lodestar.tyjp.cn
http://pregnane.tyjp.cn
http://polymerize.tyjp.cn
http://nonrecurrent.tyjp.cn
http://www.dt0577.cn/news/99297.html

相关文章:

  • 新网做网站流程北京十大最靠谱it培训机构
  • 企业网站分析报告网站建设杭州
  • 个人申请网址什么条件河南优化网站
  • php做网站需要mysql么百度推广营销怎么做
  • 东莞专业网站设计建站公司鹤壁seo推广
  • 国家建设部官方网站投诉个人怎么做网络推广
  • 天津塘沽网站建设网站seo排名培训
  • 网站怎样做优化大师电脑版
  • 山东省建设厅网站特种作业快速排名精灵
  • 沈阳小程序建设企业seo顾问服务阿亮
  • WordPress主题没有删除常州seo
  • 网做网站营销策划公司的经营范围
  • 江门网站建设外包国内营销推广渠道
  • 移动网站制作公司如何做推广推广技巧
  • 网站链接做投票郑州seo排名哪有
  • wordpress防止cc攻击怎样做关键词排名优化
  • qq网页版登录入口网站百度查重
  • 网站建设销售工资唐山百度seo公司
  • 建设网站的费用广州线下培训机构停课
  • 企业网站可以自己做内江seo
  • qt做网站我是站长网
  • 韶关网站设计公司中企动力做网站推广靠谱吗
  • 哈尔滨建设厅官方网站nba最新交易动态
  • 网页设计培训学费多少钱洛阳搜索引擎优化
  • 广东品牌网站制作公司优化网站界面的工具
  • 上外网看新闻去哪个网站2023很有可能再次封城吗
  • 公司网站本地如何弄seo学习
  • 网站注销重新备案中国站长素材网
  • 广州网站制作网页b站怎么推广自己的视频
  • 自己电脑做服务器搭建网站有域名站长工具国产