forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep track of user last visited time
- Loading branch information
1 parent
1b4fdd8
commit 79e6773
Showing
10 changed files
with
206 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use App\Models\User; | ||
|
||
class SyncUserActivedAt extends Command | ||
{ | ||
protected $signature = 'larabbs:sync-user-actived-at'; | ||
protected $description = '将用户最后登录时间从 Redis 同步到数据库中'; | ||
|
||
public function handle(User $user) | ||
{ | ||
$user->syncUserActivedAt(); | ||
$this->info("同步成功!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Auth; | ||
|
||
class RecordLastActivedTime | ||
{ | ||
public function handle($request, Closure $next) | ||
{ | ||
// 如果是登录用户的话 | ||
if (Auth::check()) { | ||
// 记录最后登录时间 | ||
Auth::user()->recordLastActivedAt(); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace App\Models\Traits; | ||
|
||
use Redis; | ||
use Carbon\Carbon; | ||
|
||
trait LastActivedAtHelper | ||
{ | ||
// 缓存相关 | ||
protected $hash_prefix = 'larabbs_last_actived_at_'; | ||
protected $field_prefix = 'user_'; | ||
|
||
public function recordLastActivedAt() | ||
{ | ||
// 获取今日 Redis 哈希表名称,如:larabbs_last_actived_at_2017-10-21 | ||
$hash = $this->getHashFromDateString(Carbon::now()->toDateString()); | ||
|
||
// 字段名称,如:user_1 | ||
$field = $this->getHashField(); | ||
|
||
// 当前时间,如:2017-10-21 08:35:15 | ||
$now = Carbon::now()->toDateTimeString(); | ||
|
||
// 数据写入 Redis ,字段已存在会被更新 | ||
Redis::hSet($hash, $field, $now); | ||
} | ||
|
||
public function syncUserActivedAt() | ||
{ | ||
// 获取昨日的哈希表名称,如:larabbs_last_actived_at_2017-10-21 | ||
$hash = $this->getHashFromDateString(Carbon::now()->subDay()->toDateString()); | ||
|
||
// 从 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_actived_at = $actived_at; | ||
$user->save(); | ||
} | ||
} | ||
|
||
// 以数据库为中心的存储,既已同步,即可删除 | ||
Redis::del($hash); | ||
} | ||
|
||
public function getLastActivedAtAttribute($value) | ||
{ | ||
// 获取今日对应的哈希表名称 | ||
$hash = $this->getHashFromDateString(Carbon::now()->toDateString()); | ||
|
||
// 字段名称,如:user_1 | ||
$field = $this->getHashField(); | ||
|
||
// 三元运算符,优先选择 Redis 的数据,否则使用数据库中 | ||
$datetime = Redis::hGet($hash, $field) ? : $value; | ||
|
||
// 如果存在的话,返回时间对应的 Carbon 实体 | ||
if ($datetime) { | ||
return new Carbon($datetime); | ||
} else { | ||
// 否则使用用户注册时间 | ||
return $this->created_at; | ||
} | ||
} | ||
|
||
public function getHashFromDateString($date) | ||
{ | ||
// Redis 哈希表的命名,如:larabbs_last_actived_at_2017-10-21 | ||
return $this->hash_prefix . $date; | ||
} | ||
|
||
public function getHashField() | ||
{ | ||
// 字段名称,如:user_1 | ||
return $this->field_prefix . $this->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
database/migrations/2017_11_01_165559_add_last_actived_at_to_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddLastActivedAtToUsersTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->timestamp('last_actived_at')->nullable(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn('last_actived_at'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"site_name":"ss - Powered by LaraBBS","contact_email":"[email protected]","seo_description":"sdf","seo_keyword":null} |