use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
/**
* @param $key string
* @param $memberId integer
* @throws \ShallBuy\LaravelBase\Common\Exception\Exceptions\BusinessException
*/
private static function memberIdExists($key, $memberId) {
if (Redis::sismember($key, $memberId)) {
$len = Redis::scard($key);
$memberIds = Redis::smembers($key);
Log::info(sprintf("len=%d, members=[%s]", $len, implode(',', $memberIds)));
throw ExceptionFactoryFacade::business(CodeMessageConstant::ERROR_MARQUEE_UNSUBSCRIBED);
}
}
public function unsubscribe($memberId) {
$key = Marquee::CACHE_HIDE_MEMBERS_LIST;
/** @var $status \Predis\Response\Status */
$status = Redis::type($key);
$type = $status->getPayload();
// 已存在重名的list
if (strncmp($type, "list", 4) == 0) {
$len = Redis::llen($key);
$a = Redis::lrange($key, 0, $len);
Redis::del($key);
foreach ($a as $id) {
Redis::sadd($key, $id);
}
}
// 除set以外的其他类型, 直接删除
else if (strncmp($type, "set", 3) != 0) {
Redis::del($key);
return Redis::sadd($key, $memberId);
}
self::memberIdExists($key, $memberId);
return Redis::sadd($key, $memberId);
}
|