Skip to content

Commit

Permalink
feat(CacheMeService): ttl defined like seconds instead of minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Feb 6, 2024
1 parent d7912fb commit e2eb679
Show file tree
Hide file tree
Showing 49 changed files with 106 additions and 68 deletions.
44 changes: 38 additions & 6 deletions src/Cache/Constants/CacheExpirations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,49 @@
namespace LaraStrict\Cache\Constants;

/**
* Stores on minutes.
* Stores in seconds
*/
class CacheExpirations
final class CacheExpirations
{
/**
* 720 minutes - 12 hours.
* minute in seconds
*/
final public const HalfDay = 720;
public const Minute = 60;

/**
* 1 days.
* hour in seconds
*/
final public const Long = 44640;
public const Hour = 60 * self::Minute;

/**
* day in seconds
*/
public const Day = 24 * self::Hour;

/**
* week in seconds
*/
public const Week = 7 * self::Day;

/**
* average month in seconds
*/
public const Month = 2_629_800;

/**
* average year in seconds
*/
public const Year = 31_557_600;

/**
* Fixed value
*
* @deprecated
*/
public const Long = self::Month;

/**
* @deprecated
*/
public const HalfDay = 12 * self::Hour;
}
4 changes: 2 additions & 2 deletions src/Cache/Contracts/CacheMeServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function get(
string $key,
Closure $getValue,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true
): mixed;
Expand All @@ -36,7 +36,7 @@ public function set(
string $key,
mixed $value,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true
): void;
Expand Down
24 changes: 15 additions & 9 deletions src/Cache/Services/CacheMeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@ public function __construct(

/**
* Stores in memory and uses the default cache.
*
* @param ?int $minutes - deprecated use $seconds
*/
public function get(
string $key,
Closure $getValue,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true
bool $log = true,
?int $minutes = null
): mixed {
if ($strategy === CacheMeStrategy::None) {
return $this->container->call($getValue);
}
if ($minutes !== null) {
$seconds = $minutes;
}

$value = null;
$repositories = $this->repositories($tags, $strategy);
Expand Down Expand Up @@ -72,7 +78,7 @@ public function get(
key: $key,
value: $value,
tags: $tags,
minutes: $minutes,
seconds: $seconds,
log: false
);
}
Expand All @@ -91,7 +97,7 @@ public function get(
key: $key,
value: $value,
tags: $tags,
minutes: $minutes,
seconds: $seconds,
log: $log
);
}
Expand All @@ -107,7 +113,7 @@ public function set(
string $key,
mixed $value,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true
): void {
Expand All @@ -116,7 +122,7 @@ public function set(
key: $key,
value: $value,
tags: $tags,
minutes: $minutes,
seconds: $seconds,
log: $log
);
}
Expand Down Expand Up @@ -244,7 +250,7 @@ protected function store(
string $key,
mixed $value,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
bool $log = true
): void {
if ($repositories === []) {
Expand All @@ -254,14 +260,14 @@ protected function store(
if ($log) {
$this->logger->debug('Storing cache', [
'key' => $key,
'minutes' => $minutes,
'seconds' => $seconds,
'tags' => $tags,
'store' => array_map(static fn (CacheContract $store) => $store->getStore()::class, $repositories),
]);
}

foreach ($repositories as $store) {
$store->set($key, $value, $minutes);
$store->set($key, $value, $seconds);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Context/Services/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function set(AbstractContext $context, ContextValueContract $value): void
key: $fullCacheKey,
value: $value,
tags: $this->getTags($context),
minutes: $context->getCacheTtl(),
seconds: $context->getCacheTtl(),
);
}

Expand All @@ -57,7 +57,7 @@ public function setWithoutCache(AbstractContext $context, ContextValueContract $
key: $fullCacheKey,
value: $value,
tags: $this->getTags($context),
minutes: $context->getCacheTtl(),
seconds: $context->getCacheTtl(),
strategy: CacheMeStrategy::Memory,
);
}
Expand All @@ -70,7 +70,7 @@ public function get(AbstractContext $context, Closure $createState): ContextValu
key: $fullCacheKey,
getValue: $createState,
tags: $this->getTags($context),
minutes: $context->getCacheTtl(),
seconds: $context->getCacheTtl(),
strategy: $this->cacheStrategy($context)
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Testing/Cache/Contracts/CacheMeServiceContractAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function get(
string $key,
Closure $getValue,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true,
): mixed {
Expand All @@ -55,7 +55,7 @@ public function get(

Assert::assertEquals($expectation->key, $key, $message);
Assert::assertEquals($expectation->tags, $tags, $message);
Assert::assertEquals($expectation->minutes, $minutes, $message);
Assert::assertEquals($expectation->minutes, $seconds, $message);
Assert::assertEquals($expectation->strategy, $strategy, $message);
Assert::assertEquals($expectation->log, $log, $message);

Expand All @@ -71,7 +71,7 @@ public function set(
string $key,
mixed $value,
array $tags = [],
int $minutes = CacheExpirations::HalfDay,
int $seconds = CacheExpirations::Day,
CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
bool $log = true,
): void {
Expand All @@ -81,7 +81,7 @@ public function set(
Assert::assertEquals($expectation->key, $key, $message);
Assert::assertEquals($expectation->value, $value, $message);
Assert::assertEquals($expectation->tags, $tags, $message);
Assert::assertEquals($expectation->minutes, $minutes, $message);
Assert::assertEquals($expectation->minutes, $seconds, $message);
Assert::assertEquals($expectation->strategy, $strategy, $message);
Assert::assertEquals($expectation->log, $log, $message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CacheMeServiceContractGetExpectation
public function __construct(
public readonly string $key,
public readonly array $tags = [],
public readonly int $minutes = CacheExpirations::HalfDay,
public readonly int $minutes = CacheExpirations::Day,
public readonly CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
public readonly ?Closure $callGetValueHook = null,
public readonly bool $log = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(
public readonly string $key,
public readonly mixed $value,
public readonly array $tags = [],
public readonly int $minutes = CacheExpirations::HalfDay,
public readonly int $minutes = CacheExpirations::Day,
public readonly CacheMeStrategy $strategy = CacheMeStrategy::MemoryAndRepository,
public readonly bool $log = true,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function generateData(): array
key: self::Key,
value: self::Return,
tags: [self::Return],
minutes: 230,
seconds: 230,
strategy: CacheMeStrategy::Memory,
),
checkResult: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestActionContractAssert extends \LaraStrict\Testing\Assert\AbstractExpect
string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
\LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class TestActionContractExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class TestActionExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TestReturnActionContractAssert extends \LaraStrict\Testing\Assert\Abstract
string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
\LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TestReturnActionContractExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TestReturnActionExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TestReturnIntersectionActionExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TestReturnRequiredActionExpectation
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::Day,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function execute(
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
int $constantClass = CacheExpirations::Day,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
Expand Down
Loading

0 comments on commit e2eb679

Please sign in to comment.