PHP code example of marko / cache
1. Go to this page and download the library: Download marko/cache library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
marko / cache example snippets
use Marko\Cache\Contracts\CacheInterface;
class ProductService
{
public function __construct(
private CacheInterface $cache,
) {}
public function getProduct(int $id): Product
{
$key = "product.$id";
if ($this->cache->has($key)) {
return $this->cache->get($key);
}
$product = $this->repository->find($id);
$this->cache->set($key, $product, ttl: 3600);
return $product;
}
}