This commit is contained in:
me
2026-07-26 15:42:12 +07:00
parent 7448b5ea2a
commit 4530ece39d
+63
View File
@@ -0,0 +1,63 @@
<?php
/*
* Wraper for Symfony Cache
* use with Cache Adapter
* https://symfony.com/doc/current/components/cache.html
*/
namespace Nightmare\Cache;
class Cache
{
private $adapter;
public function __construct($adapter) {
$this->adapter = $adapter;
}
public function has($key)
{
return $this->adapter->hasItem($key);
}
public function get($key, $default = null)
{
$item = $this->adapter->getItem($key);
return $item->isHit() ? $item->get() : $default;
}
public function set($key, $value, $ttl = null)
{
$item = $this->adapter->getItem($key);
$item->expiresAfter($ttl);
$item->set($value);
return $this->adapter->save($item);
}
public function delete($key)
{
return $this->adapter->deleteItem($key);
}
public function clear()
{
return $this->adapter->clear();
}
public function r($key, $callback, $ttl = null)
{
$item = $this->adapter->getItem($key);
if ($item->isHit()) {
return $item->get();
}
$item->expiresAfter($ttl);
$item->set($callback());
$this->adapter->save($item);
return $item->get();
}
}