drsdre / yii2-redis-counter
Redis Counter 使用 Redis 存储实现快速原子计数器。
1.0
2016-01-16 22:59 UTC
Requires
- yiisoft/yii2: *
- yiisoft/yii2-bootstrap: *
- yiisoft/yii2-redis: ~2.0.0
This package is auto-updated.
Last update: 2024-08-29 04:09:08 UTC
README
Redis Counter 使用 Redis 存储实现快速原子计数器。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist drsdre/yii2-redis-counter "*"
或者在您的 composer.json
文件的 require 部分添加
"drsdre/yii2-redis-counter": "*"
用法
您需要将客户端配置为应用程序组件
'components' => [ 'redisCounter' => [ 'class' => 'drsdre\redis\Counter', ], ... ]
可选地,可以添加参数 'redis' 来指定特定的 Redis 连接。
用法
扩展安装后,在您的代码中使用它,如下所示:
// Create a hourly counter $counter_key = 'hourly_statistics'; // Check if counter is setup if (Yii::$app->redisCounter->exists($counter_key)) { // Atomic increment counter with 1 Yii::$app->redisCounter->incr($counter_key); } else { // Create counter set value to 1 and let it exist for 1 hour Yii::$app->redisCounter->set($counter_key, 1, 60*60); }