laravel-learn-bbs/app/Traits/LastActivedAtHelper.php

80 lines
2.2 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
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/2
* Time: 22:29
*/
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
/**
* Trait LastActivedAtHelper
* @property int id
* @package App\Traits
*/
trait LastActivedAtHelper
{
// 缓存相关
protected $hash_prefix = 'larabbs_last_actived_at_';
protected $field_prefix = 'user_';
public function recordLastActivedAt()
{
// 获取今天的日期
$date = Carbon::now()->toDateString();
// Redis 哈希表的命名larabbs_last_actived_at_2017-10-21
$hash = $this->hash_prefix . $date;
// 字段名称user_1
$field = $this->field_prefix . $this->id;
// 当前时间2017-10-21 08:35:15
$now = Carbon::now()->toDateTimeString();
// 数据写入 Redis ,字段已存在会被更新
Redis::hSet($hash, $field, $now);
}
public function syncUserActivedAt()
{
// 获取昨天的日期格式如2017-10-21
$yesterday_date = Carbon::yesterday()->toDateString();
// Redis 哈希表的命名larabbs_last_actived_at_2017-10-21
$hash = $this->hash_prefix . $yesterday_date;
// 从 Redis 中获取所有哈希表里的数据
$dates = Redis::hGetAll($hash);
// 遍历,并同步到数据库中
foreach ($dates as $user_id => $actived_at) {
// 会将 `user_1` 转换为 1
$user_id = str_replace($this->field_prefix, '', $user_id);
// 只有当用户存在时才更新到数据库中
if ($user = $this->find($user_id)) {
$user->last_active_at = $actived_at;
$user->save();
}
}
// 以数据库为中心的存储,既已同步,即可删除
Redis::del($hash);
}
public function getLastActivedAt($value)
{
$date = Carbon::now()->toDateString();
// Redis 哈希表的命名larabbs_last_actived_at_2017-10-21
$hash = $this->hash_prefix . $date;
$field = $this->field_prefix . $this->id;
$date = Redis::hGet($hash, $field) ?: $value;
return $date ? new Carbon($date) : $this->created_at;
}
}