Skip to content

Commit

Permalink
Update PostFilter constructor to accept a nullable cache handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Thavarshan committed Apr 10, 2024
1 parent 7485188 commit 2607460
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ You can apply filters to your Eloquent queries like so:
```php
use App\Models\Post;

$filter = new PostFilter(request());
$filter = new PostFilter(request(), cache());
$posts = Post::query()
->filter($filter)
->get();
Expand Down
36 changes: 32 additions & 4 deletions src/Filterable/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ abstract class Filter implements FilterInterface
/**
* Create a new filter instance.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Cache\Repository|null $cache
*
* @return void
*/
public function __construct(
protected Request $request,
protected Cache $cache
protected ?Cache $cache = null
) {
}

Expand Down Expand Up @@ -195,7 +195,7 @@ protected function applyFilterables(): void
return;
}

$this->cache->remember(
$this->getCacheHandler()->remember(
$this->buildCacheKey(),
Carbon::now()->addMinutes($this->getCacheExpiration()),
function () {
Expand Down Expand Up @@ -465,4 +465,32 @@ public function clearCache(): void
{
$this->cache->forget($this->buildCacheKey());
}

/**
* Get the value of cache
*
* @return \Illuminate\Contracts\Cache\Repository
*/
public function getCacheHandler(): Cache
{
if (is_null($this->cache)) {
$this->cache = cache();
}

return $this->cache;
}

/**
* Set the value of cache
*
* @param \Illuminate\Contracts\Cache\Repository $cache
*
* @return self
*/
public function setCacheHandler(Cache $cache)
{
$this->cache = $cache;

return $this;
}
}

0 comments on commit 2607460

Please sign in to comment.