| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Cache;
- use App\Libs\Utils;
- use Illuminate\Support\Facades\Redis;
- class WechatStateCache
- {
- /**
- * 写入扫码 state
- *
- * @param string $state
- * @param int $ttl 有效期(秒)
- * @return void
- */
- public static function set(string $state, int $ttl)
- {
- Redis::setex(Utils::getCacheKey('wechat.state', [$state]), $ttl, '1');
- }
- /**
- * 校验并消费 state(一次性)
- *
- * @param string $state
- * @return bool
- */
- public static function pull(string $state): bool
- {
- if ($state === '') {
- return false;
- }
- $cacheKey = Utils::getCacheKey('wechat.state', [$state]);
- $value = Redis::get($cacheKey);
- if ($value === null || $value === '') {
- return false;
- }
- Redis::del($cacheKey);
- return true;
- }
- }
|