-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
104 changed files
with
3,761 additions
and
486 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
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,126 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Post; | ||
use App\Repositories\Contracts\PostRepository; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Redis; | ||
|
||
class SavePostViewCount extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'view_count:save'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Save posts view count into DB and flush cache'; | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
protected $cacheKeyPrefix; | ||
|
||
/** | ||
* @var PostRepository | ||
*/ | ||
protected $postRepo; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @param PostRepository $postRepo | ||
* @return void | ||
*/ | ||
public function __construct(PostRepository $postRepo) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->postRepo = $postRepo; | ||
|
||
$this->cacheKeyPrefix = config('blog.counter.cache_key'); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$data = $this->savePostViewCount(); | ||
|
||
$this->info("Post view_count save successfully at " . Carbon::now()->toDateTimeString()); | ||
$this->table(['Post ID', 'Increase Count'], $data); | ||
} | ||
|
||
/** | ||
* Retrieve all post view_count from cache and save into DB | ||
* | ||
* @return array | ||
*/ | ||
protected function savePostViewCount() | ||
{ | ||
$results = []; | ||
|
||
// Retrieve all post id | ||
$this->postRepo | ||
->getModel() | ||
->select('id') | ||
->chunk(100, function ($posts) use (&$results) { | ||
foreach ($posts as $post) { | ||
if ($count = $this->getCacheCount($post->id)) { | ||
$post->increment('view_count', $count); | ||
|
||
array_push($results, [$post->id, $count]); | ||
|
||
$this->flushCache($this->cacheKey($post->id)); | ||
} | ||
} | ||
}); | ||
|
||
return $results; | ||
} | ||
|
||
/** | ||
* Get post view_count from cache | ||
* | ||
* @param $id | ||
* @return mixed | ||
*/ | ||
protected function getCacheCount($id) | ||
{ | ||
return Cache::get($this->cacheKey($id)); | ||
} | ||
|
||
/** | ||
* Get cache key | ||
* | ||
* @param $id | ||
* @return string | ||
*/ | ||
protected function cacheKey($id) | ||
{ | ||
return $this->cacheKeyPrefix . $id; | ||
} | ||
|
||
/** | ||
* Flush post view_count in cache by key | ||
* | ||
* @param $key | ||
* @return bool | ||
*/ | ||
protected function flushCache($key) | ||
{ | ||
return Cache::forget($key); | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace App\Contracts; | ||
|
||
/** | ||
* Interface ContentableInterface | ||
* @package App\Contracts | ||
*/ | ||
interface ContentableInterface | ||
{ | ||
/** | ||
* Get raw content in markdown syntax. | ||
* | ||
* @return string | ||
*/ | ||
public function getRawContent(); | ||
|
||
/** | ||
* Get primary key in 'contents' table. | ||
* | ||
* @return int | ||
*/ | ||
public function getContentId(); | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Broadcasting\PresenceChannel; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
|
||
/** | ||
* Class PostViewEvent | ||
* @package App\Events | ||
*/ | ||
class PostViewEvent | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* @var | ||
*/ | ||
public $postId; | ||
|
||
/** | ||
* PostViewEvent constructor. | ||
* @param $postId | ||
*/ | ||
public function __construct($postId) | ||
{ | ||
$this->postId = $postId; | ||
} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return Channel|array | ||
*/ | ||
public function broadcastOn() | ||
{ | ||
return new PrivateChannel('channel-name'); | ||
} | ||
} |
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
Oops, something went wrong.