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
5 changed files
with
122 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
use Psr\SimpleCache\InvalidArgumentException; | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
function cache( | ||
string|int|Stringable|array $key, | ||
mixed $value = null, | ||
int|DateInterval|null $ttl = null, | ||
): mixed { | ||
/** | ||
* @var CacheInterface $cache | ||
Check failure on line 17 in src/cache.php GitHub Actions / PHP 8.1-ubuntu-latestUnnecessaryVarAnnotation
Check failure on line 17 in src/cache.php GitHub Actions / PHP 8.2-ubuntu-latestUnnecessaryVarAnnotation
|
||
*/ | ||
$cache = container(CacheInterface::class); | ||
|
||
$key = is_array($key) ? strtr(json_encode($key), '{}()/\@:', '________') : (string) $key; | ||
|
||
if ($cache->has($key)) { | ||
return $cache->get($key); | ||
} | ||
|
||
$value = is_callable($value) ? $value() : $value; | ||
$cache->set($key, $value, $ttl); | ||
|
||
return $value; | ||
} |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Xepozz\Shortcut\Tests; | ||
|
||
use DateInterval; | ||
use Psr\SimpleCache\CacheInterface; | ||
use Xepozz\Shortcut\State; | ||
use Xepozz\Shortcut\Tests\Support\StringableClass; | ||
use Yiisoft\Cache\NullCache; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class CacheTest extends FunctionsTestCase | ||
{ | ||
public function testFunctionExist() | ||
{ | ||
$this->assertTrue(function_exists('cache')); | ||
} | ||
|
||
public function testCache(): void | ||
{ | ||
$this->initEnvironment(); | ||
|
||
$result = cache('key', 'value'); | ||
$this->assertEquals('value', $result); | ||
|
||
|
||
$result = cache('key', 'value', 3600); | ||
$this->assertEquals('value', $result); | ||
|
||
$result = cache('key', 'value', new DateInterval('PT1H')); | ||
$this->assertEquals('value', $result); | ||
|
||
$result = cache('key', fn () => 'value'); | ||
$this->assertEquals('value', $result); | ||
|
||
$result = cache(new StringableClass('key'), fn () => 'value'); | ||
$this->assertEquals('value', $result); | ||
|
||
$result = cache(12345, fn () => 'value'); | ||
$this->assertEquals('value', $result); | ||
|
||
$result = cache(['key' => 'value', '!@#$%^&*()_+`' => '_)(*&^%$#@!~`'], fn () => 'value'); | ||
$this->assertEquals('value', $result); | ||
} | ||
|
||
public function bootstrapFiles(): iterable | ||
{ | ||
yield __DIR__ . '/../src/container.php'; | ||
yield __DIR__ . '/../src/cache.php'; | ||
} | ||
|
||
protected function initEnvironment(): void | ||
{ | ||
State::$container = new SimpleContainer([ | ||
CacheInterface::class => new NullCache(), | ||
]); | ||
} | ||
} |
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