🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

github.com/liuhangkaixcode/dis-redis

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/liuhangkaixcode/dis-redis

v0.0.0-20201008050843-c9ad4174d159
Version published
Created

dis-redis 基于redis的分布式锁

使用方法

import  "github.com/liuhangkaixcode/dis-redis"
lockIF, e := dis_redis.NewRedisLock(100, pool, "cisco", timeout)
	if e!=nil {
		panic("参数异常")
	}

//v 是 setnx 设置的value
//尝试加锁,如果有错误返回,加锁失败。
e := lockIF.Lock(v)
	if e!=nil {
		return
	}

....这是处理业务

//释放锁 
//v 是之前setnx 设置的value
lockIF.Unlock(v)

demo1 TestNewRedisLock 模拟抢票过程

demo2 TestGinServer 模拟分布式秒杀过程

ab.exe -c 100 -n 1000 http://127.0.0.1:9091/t1?port=9091

ab.exe -c 100 -n 1000 http://127.0.0.1:9092/t1?port=9092

下面是数据库的结构

mysql> select * from good;
+----+---------+--------+-----+
| id | good_id | name   | num |
+----+---------+--------+-----+
|  1 |       1 | orange | 111 |
+----+---------+--------+-----+
mysql> desc ord;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| order_no | varchar(100)     | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+

FAQs

Package last updated on 08 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts