hyperf-chat/app/Cache/LastMessage.php

62 lines
1.5 KiB
PHP
Raw Normal View History

2021-05-20 22:23:48 +08:00
<?php
namespace App\Cache;
use App\Cache\Repository\HashRedis;
2021-07-29 23:30:42 +08:00
use App\Traits\StaticInstance;
2021-05-20 22:23:48 +08:00
/**
* 聊天最新消息 - 缓存助手
*
* @package App\Cache
*/
class LastMessage extends HashRedis
{
2021-07-29 23:30:42 +08:00
use StaticInstance;
2021-05-20 22:23:48 +08:00
public $name = 'last-message';
/**
* 保存最后一条缓存信息
*
* @param int $type 聊天类型[1:私信;2:群聊;]
* @param int $sender 发送者ID
* @param int $receive 接收者ID
* @param array $message
*/
public function save(int $type, int $sender, int $receive, array $message)
{
$this->add($this->flag($type, $sender, $receive), json_encode($message));
}
/**
* 读取最后一条缓存信息
*
* @param int $type 聊天类型[1:私信;2:群聊;3:机器人;]
* @param int $sender 发送者ID
* @param int $receive 接收者ID
* @return array
*/
2021-05-22 20:54:30 +08:00
public function read(int $type, int $sender, int $receive): array
2021-05-20 22:23:48 +08:00
{
$message = $this->get($this->flag($type, $sender, $receive));
return $message ? json_decode($message, true) : [];
}
/**
* 获取 Hash 成员 key
*
* @return string
*/
2021-08-20 22:38:46 +08:00
public function flag(int $type, int $sender, int $receive): string
2021-05-20 22:23:48 +08:00
{
// 群聊信息(非私信)发送者ID重置为零
if ($type == 2) $sender = 0;
[$sender, $receive] = $sender <= $receive ? [$sender, $receive] : [$receive, $sender];
return sprintf("%s_%s_%s", $type, $sender, $receive);
}
}