PHP code example of switon / cache
1. Go to this page and download the library: Download switon/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/ */
switon / cache example snippets
use Psr\SimpleCache\CacheInterface;
use Switon\Core\Attribute\Autowired;
class ProductService
{
#[Autowired] protected CacheInterface $cache;
public function getFeatured(): array
{
$miss = new \stdClass();
$products = $this->cache->get('featured_products', $miss);
if ($products === $miss) {
$products = $this->loadFeaturedProducts();
$this->cache->set('featured_products', $products, 1800);
}
return $products;
}
}