28 lines
564 B
PHP
28 lines
564 B
PHP
<?php
|
||
|
||
namespace app\common\services;
|
||
|
||
use think\Cache;
|
||
|
||
class RedisService
|
||
{
|
||
protected $redis;
|
||
|
||
/**
|
||
* @param int $db Redis数据库编号,默认 0
|
||
*/
|
||
public function __construct(int $db = 0)
|
||
{
|
||
$this->redis = Cache::store('redis')->handler();
|
||
$this->redis->select($db);
|
||
}
|
||
|
||
public function incrWithExpire(string $key, int $expireSeconds = 86400)
|
||
{
|
||
$val = $this->redis->incr($key);
|
||
if ($val === 1) {
|
||
$this->redis->expire($key, $expireSeconds);
|
||
}
|
||
return $val;
|
||
}
|
||
} |