zx/app/webscoket/Ping.php

109 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\webscoket;
use think\facade\Cache;
/**
* Class Ping
* @package app\webscoket
* @author xaboy
* @day 2020-04-29
*/
class Ping
{
/**
* @var \Redis
*/
protected $redis;
const CACHE_PINK_KEY = 'ws.p.';
const CACHE_SET_KEY = 'ws.s';
/**
* Ping constructor.
*/
public function __construct()
{
$this->redis = Cache::store('redis')->handler();
$this->destroy();
}
/**
* @param $id
* @param $time
* @param int $timeout
* @author xaboy
* @day 2020-04-29
*/
public function createPing($id, $time, $timeout = 0)
{
$this->updateTime($id, $time, $timeout);
$this->redis->sAdd(self::CACHE_SET_KEY, $id);
}
/**
* @param $id
* @param $time
* @param int $timeout
* @author xaboy
* @day 2020-04-29
*/
public function updateTime($id, $time, $timeout = 0)
{
$this->redis->set(self::CACHE_PINK_KEY . $id, $time, $timeout);
}
/**
* @param $id
* @author xaboy
* @day 2020-05-06
*/
public function removePing($id)
{
$this->redis->del(self::CACHE_PINK_KEY . $id);
$this->redis->del(self::CACHE_SET_KEY, $id);
}
/**
* @param $id
* @return bool|string
* @author xaboy
* @day 2020-04-29
*/
public function getLastTime($id)
{
return $this->redis->get(self::CACHE_PINK_KEY . $id);
}
/**
* @author xaboy
* @day 2020-04-29
*/
public function destroy()
{
$members = $this->redis->sMembers(self::CACHE_SET_KEY) ?: [];
foreach ($members as $k => $member) {
$members[$k] = self::CACHE_PINK_KEY . $member;
}
if (count($members))
$this->redis->del(self::CACHE_SET_KEY, ...$members);
}
}