1. Go to this page and download the library: Download phpro/annotated-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/ */
namespace My;
use Phpro\AnnotatedCache\Annotation\Cacheable;
use Phpro\AnnotatedCache\Annotation\CacheUpdate;
use Phpro\AnnotatedCache\Annotation\CacheEvict;
class Service
{
/**
* @Cacheable(pools="products", key="sku", tags="product-detail", ttl=300)
*/
public function getProduct($sku, $type = 'book')
{
// fetch a product from a repository or whatever
$product = $this->productRepository->getByType($sku, 'book');
return $product;
}
/**
* @CacheEvict(pools="products", key="product.getSku()", tags="product-overview,product-reports")
*/
public function removeProduct(Product $product)
{
// saving product ...
}
/**
* @CacheUpdate(pools="products", key="product.getSku()", tags="product-detail", ttl=300)
*/
public function updateProduct(Product $product)
{
// saving product....
return $product;
}
}
namespace My\Manager;
use My\Model\Product;
use Phpro\AnnotatedCache\Annotation\Cacheable;
class ProductManager
{
/**
* @Cacheable(pools="products", key="sku", tags="book-detail", ttl=500)
*/
public function getProduct($sku, $type = 'book')
{
// fetch a product from a repository or whatever
$product = $this->productRepository->getByType($sku, 'book');
return $product;
}
}
namespace My\Manager;
use My\Model\Product;
use Phpro\AnnotatedCache\Annotation\CacheEvict;
class ProductManager
{
/**
* @CacheEvict(pools="products", key="product.getSku()", tags="book-list")
*/
public function removeProduct(Product $product)
{
// saving product ...
}
}
namespace My\Manager;
use My\Model\Product;
use Phpro\AnnotatedCache\Annotation\CacheUpdate;
class ProductManager
{
/**
* @CacheUpdate(pools="products", key="product.getSku()", tags="product-detail", ttl=300)
*/
public function updateProduct(Product $product)
{
// saving product....
return $product;
}
}
/**
* @CacheUpdate(pools="products", key="product.getSku()")
*/
public function updateProduct(Product $product)
{
// do something
}
/**
* @Cacheable(pools="products", key="interception.getMethod() ~ id")
*/
public function getProduct(int $id)
{
// do something
}
use Phpro\AnnotatedCache\Factory;
use Phpro\AnnotatedCache\Collector\MemoryResultCollector;
// Instantiate pool
$resultCollector = new MemoryResultCollector();
$cacheHandler = Factory::createCacheHandler($poolManager, $resultCollector);
// Instantiate proxy service ...
$myService->fetchSomethingCached();
$results = $resultCollector->getResults();