redis并发量最大是多少


当前第2页 返回上一页

redis中命令是满足原子性的,因此在值为阿拉伯数字的时候,我可以将get和set命令修改为incr或者incrby来解决这个问题,下面的代码开启两个终端同时执行,得到的结果是满足我们预期的2000。

1

2

3

4

5

6

<?phprequire "vendor/autoload.php";$client = new Predis\Client([

'scheme' => 'tcp',

'host' => '127.0.0.1',

'port' => 6379,]);for ($i = 0; $i < 1000; $i++) { $client->incr("name");

$client->expire("name", 10800);

usleep(10000);}

确实可行,效果还不错,这里写了个例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<?phprequire "vendor/autoload.php";$client = new Predis\Client([

'scheme' => 'tcp',

'host' => '127.0.0.1',

'port' => 6379,]);class RedisLock{ public $objRedis = null;

public $timeout = 3;

/** * @desc 设置redis实例 * * @param obj object | redis实例 */

public function __construct($obj)

{ $this->objRedis = $obj;

} /** * @desc 获取锁键名 */

public function getLockCacheKey($key)

{ return "lock_{$key}";

} /** * @desc 获取锁 * * @param key string | 要上锁的键名 * @param timeout int | 上锁时间 */

public function getLock($key, $timeout = NULL)

{ $timeout = $timeout ? $timeout : $this->timeout;

$lockCacheKey = $this->getLockCacheKey($key);

$expireAt = time() + $timeout;

$isGet = (bool)$this->objRedis->setnx($lockCacheKey, $expireAt);

if ($isGet) { return $expireAt;

} while (1) { usleep(10);

$time = time();

$oldExpire = $this->objRedis->get($lockCacheKey);

if ($oldExpire >= $time) { continue;

} $newExpire = $time + $timeout;

$expireAt = $this->objRedis->getset($lockCacheKey, $newExpire);

if ($oldExpire != $expireAt) { continue;

} $isGet = $newExpire;

break;

} return $isGet;

} /** * @desc 释放锁 * * @param key string | 加锁的字段 * @param newExpire int | 加锁的截止时间 * * @return bool | 是否释放成功 */

public function releaseLock($key, $newExpire)

{ $lockCacheKey = $this->getLockCacheKey($key);

if ($newExpire >= time()) { return $this->objRedis->del($lockCacheKey);

} return true;

}

}$start_time = microtime(true);$lock = new RedisLock($client);$key = "name";for ($i = 0; $i < 10000; $i++) { $newExpire = $lock->getLock($key);

$num = $client->get($key);

$num++;

$client->set($key, $num);

$lock->releaseLock($key, $newExpire);}$end_time = microtime(true);echo "花费时间 : ". ($end_time - $start_time) . "\n";

执行shell php setnx.php & php setnx.php&,最后会得到结果:

1

2

3

$ 花费时间 : 4.3004920482635

[2] + 72356 done php setnx.php# root @ ritoyan-virtual-pc in ~/PHP/redis-high-concurrency [20:23:41] $ 花费时间 : 4.4319710731506

[1] + 72355 done php setnx.php

同样循环1w次,去掉usleep,使用incr直接进行增加,耗时在2s左右。

而获取所得时候取消usleep,时间不但没减少,反而增加了,这个usleep的设置要合理,免得进程做无用的循环

总结

简单的总结下,其实redis本事是不会存在并发问题的,因为他是单进程的,再多的command都是one by one执行的。我们使用的时候,可能会出现并发问题,比如get和set这一对。

以上就是redis并发量最大是多少的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

Redis怎么安装

mac环境下Redis扩展安装与使用介绍

Redis中的双端链表实现

Redis有多少个库

Redis要点分析

window 下Redis的安装步骤

使用Redis保存用户会话session详解

为什么不用Redis做数据库

Redis cluster集群如何关闭

介绍Redis性能监控

更多相关阅读请进入《Redis》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...