負荷対策にNoSQLに一旦保存して、後からバッチでバルクインサートっていうタスクがあったので〜🐱
config/database.php
・・・ 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], 'session' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 1, ], 'member_session' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 2, ], ・・・
$ php artisan config:clear
<?php ・・・ use Illuminate\Support\Facades\Redis; use Illuminate\Support\Str; class MemberService extends SectionAbstractService { public function __construct( ・・・ ) { ・・・ $this->redis_member_session = Redis::connection("member_session"); } public function countUpViewCount(int $section_id, ?int $menber_id, ?string $member_token): void { $this->redis_member_session = Redis::connection("member_session"); $key = 'member_session_' . Str::uuid(); $data = [ 'section_id' => $member_id, 'member_id' => $member_id, 'member_token' => $member_token, 'date' => Carbon::now()->toDateTimeString() ]; $this->redis_member_session->set($key, json_encode($data)); } ・・・ }
データ確認
$ redis-cli -n 2 127.0.0.1:6379[2]> keys * 1) "member_session_305bdc3d-1c16-46ff-996f-6fc3724d06f6" 2) "member_session_1a08468b-4aa1-4ef0-a6d0-f9858c05038b" 3) "member_session_74947207-81d2-45a9-9533-6a2a24db7f88" 4) "member_session_b05720da-4436-4708-8537-011dd667a872"