2021-05-20 22:23:48 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Cache;
|
|
|
|
|
|
|
|
use App\Cache\Repository\HashRedis;
|
2022-01-16 10:29:16 +08:00
|
|
|
use App\Model\Contact;
|
2021-05-20 22:23:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 好友备注 - 缓存助手
|
|
|
|
*
|
|
|
|
* @package App\Cache
|
|
|
|
*/
|
|
|
|
class FriendRemark extends HashRedis
|
|
|
|
{
|
|
|
|
public $name = 'friend-remark';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置好友备注缓存
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $friend_id 好友ID
|
|
|
|
* @param string $remark 好友备注
|
|
|
|
*/
|
|
|
|
public function save(int $user_id, int $friend_id, string $remark)
|
|
|
|
{
|
|
|
|
$this->add($this->_flag($user_id, $friend_id), $remark);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取好友备注
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $friend_id 好友ID
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-05-22 20:54:30 +08:00
|
|
|
public function read(int $user_id, int $friend_id): string
|
2021-05-20 22:23:48 +08:00
|
|
|
{
|
|
|
|
return $this->get($this->_flag($user_id, $friend_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建用户key
|
|
|
|
*
|
|
|
|
* @param int $user_id 用户ID
|
|
|
|
* @param int $friend_id 好友ID
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-05-22 20:54:30 +08:00
|
|
|
private function _flag(int $user_id, int $friend_id): string
|
2021-05-20 22:23:48 +08:00
|
|
|
{
|
|
|
|
return "{$user_id}_{$friend_id}";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载所有数据进入缓存
|
|
|
|
*/
|
|
|
|
public function reload()
|
|
|
|
{
|
2022-01-16 10:29:16 +08:00
|
|
|
Contact::select(['id', 'user_id', 'friend_id', 'remark'])->chunk(200, function ($rows) {
|
2021-05-20 22:23:48 +08:00
|
|
|
foreach ($rows as $row) {
|
2021-12-10 23:47:46 +08:00
|
|
|
$row->remark && $this->save($row->user_id, $row->friend_id, $row->remark);
|
2021-05-20 22:23:48 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|