Skip to content

Commit

Permalink
Merge pull request #18 from syamsoul/module/cache-response
Browse files Browse the repository at this point in the history
allow to cache in `response()->json()` method
  • Loading branch information
syamsoul authored Jul 29, 2024
2 parents 0d44057 + 1b2f69c commit 5462dff
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Handler/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public function __construct(
private Handler $handler
) {}

public function json(): JsonResponse
public function json(int $cache_timeout = 0, string $extend_cache_name = ''): JsonResponse
{
$full_cache_name = 'SYAMSOUL_DATATABLE_SSP_'. request()->fullUrl();
if (!empty($extend_cache_name)) $full_cache_name .= "_$extend_cache_name";

$frontend_framework = $this->handler->frontend()->getFramework();

$data = $this->handler->data()->getData();
$data = Cache::remember(hash('sha256', $full_cache_name), $cache_timeout, fn () => $this->handler->data()->getData());

if ($frontend_framework === 'datatablejs') {
return response()->json($data);
Expand Down

0 comments on commit 5462dff

Please sign in to comment.