From a2fc2bdbbf5e240376be0fd3f7aec5e432e5482f Mon Sep 17 00:00:00 2001 From: kolirt Date: Tue, 10 Sep 2024 22:38:08 +0300 Subject: [PATCH] add caching time variations --- composer.json | 2 +- config/cacheable.php | 10 +++++++++- src/Traits/Cacheable.php | 24 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 4c96be7..c79af0b 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "homepage": "https://github.com/kolirt/laravel-cacheable", "license": "MIT", - "version": "1.0.0", + "version": "1.0.1", "authors": [ { "name": "kolirt" diff --git a/config/cacheable.php b/config/cacheable.php index 11da08d..b4257a4 100644 --- a/config/cacheable.php +++ b/config/cacheable.php @@ -3,5 +3,13 @@ return [ 'namespace' => false, - 'cache_time' => 24 * 60, // minutes + /* + |-------------------------------------------------------------------------- + | Caching time + |-------------------------------------------------------------------------- + | + | Supported values: int in minutes, "endOfDay", "endOfHour", "endOfMinute", "endOfMonth", "endOfWeek", "endOfYear" + | + */ + 'cache_time' => 24 * 60 ]; diff --git a/src/Traits/Cacheable.php b/src/Traits/Cacheable.php index 096214a..050a55f 100644 --- a/src/Traits/Cacheable.php +++ b/src/Traits/Cacheable.php @@ -18,7 +18,29 @@ trait Cacheable public function __construct() { - $this->setCacheTime(now()->addMinutes(config('cacheable.cache_time'))); + switch (config('cacheable.cache_time')) { + case 'endOfDay': + $this->setCacheTime(now()->endOfDay()); + break; + case 'endOfHour': + $this->setCacheTime(now()->endOfHour()); + break; + case 'endOfMinute': + $this->setCacheTime(now()->endOfMinute()); + break; + case 'endOfMonth': + $this->setCacheTime(now()->endOfMonth()); + break; + case 'endOfWeek': + $this->setCacheTime(now()->endOfWeek()); + break; + case 'endOfYear': + $this->setCacheTime(now()->endOfYear()); + break; + default: + $this->setCacheTime(now()->addMinutes(config('cacheable.cache_time'))); + break; + } } public function appendCacheTags(array $tags): self