1. Go to this page and download the library: Download tbbc/cache-bundle 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/ */
tbbc / cache-bundle example snippets
namespace My\Manager;
use My\Model\Product;
use Tbbc\CacheBundle\Annotation\Cacheable;
use Tbbc\CacheBundle\Annotation\CacheUpdate;
use Tbbc\CacheBundle\Annotation\CacheEvict;
class ProductManager
{
/**
* @Cacheable(caches="products", key="sku")
*/
public function getProduct($sku, $type = 'book')
{
// fetch a product from a repository or whatever
$product = $this->productRepository->getByType($sku, 'book');
return $product;
}
/**
* @CacheUpdate(caches="products", key="product.getSku()")
*/
public function updateProduct(Product $product)
{
$product = $this->productRepository->save($product);
return $product;
}
/**
* @CacheEvict(caches="products", key="product.getSku()")
*/
public function removeProduct(Product $product)
{
$product = $this->productRepository->remove($product);
}
}
// ...
public function registerBundles()
{
$bundles = array(
//...
new Tbbc\CacheBundle\TbbcCacheBundle(),
);
// ...
}
namespace My\Manager;
use My\Model\Product;
use Tbbc\CacheBundle\Annotation\Cacheable;
class ProductManager
{
/**
* @Cacheable(caches="products", key="sku")
*/
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 Tbbc\CacheBundle\Annotation\CacheEvict;
class ProductManager
{
/**
* @CacheEvict(caches="products", key="product.getSku()")
*/
public function removeProduct(Product $product)
{
// saving product ...
}
}
namespace My\Manager;
use My\Model\Product;
use Tbbc\CacheBundle\Annotation\CacheEvict;
class ProductManager
{
/**
* @CacheEvict(caches="products", allEntries=true)
*/
public function removeProduct(Product $product)
{
// saving product ...
}
}
namespace My\Manager;
use My\Model\Product;
use Tbbc\CacheBundle\Annotation\CacheUpdate;
class ProductManager
{
/**
* @CacheUpdate(caches="products", key="product.getSku()")
*/
public function updateProduct(Product $product)
{
// saving product....
return $product;
}
}
/**
* @CacheUpdate(caches="products", key="product.getSku()")
*/
public function updateProduct(Product $product)
{
// do something
}
namespace My\Manager;
use Tbbc\CacheBundle\Annotation\Cacheable;
class ProductManager
{
private $cacheManager;
private $keyGenerator;
public function __construct(CacheManagerInterface $cacheManager, KeyGeneratorInterface $keyGenerator)
{
$this->cacheManager = $cacheManager;
$this->keyGenerator = $keyGenerator;
}
public function getProduct($sku, $type = 'book')
{
$cacheKey = $this->keyGenerator->generateKey($sku);
$cache = $this->cacheManager->getCache('products');
if ($product = $cache->get($cacheKey)) {
return $product;
}
$product = $this->productRepository->findProductBySkuAndType($sku, $type);
$cache->set($cacheKey, $product);
return $product;
}
public function saveProduct(Product $product)
{
$this->productRepository->save($product);
$cacheKey = $this->keyGenerator->generateKey($product->getSku());
$cache = $this->cacheManager->getCache('products');
$cache->delete($cacheKey);
}
}
namespace My\EventListener;
use My\CacheBundle\SerializedCacheValue;
use Tbbc\CacheBundle\Event\CacheHitEvent;
use Tbbc\CacheBundle\Event\CacheUpdateEvent;
use JMS\SerializerBundle\Serializer\SerializerInterface;
use Tbbc\CacheBundle\Cache\CacheManagerInterface;
class CacheEventListener
{
private $serializer;
private $cacheManager;
public function __construct(SerializerInterface $serializer, CacheManagerInterface $cacheManager)
{
$this->serializer = $serializer;
$this->cacheManager = $cacheManager;
}
public function onAfterCacheHit(CacheHitEvent $event)
{
$value = $event->getValue();
if ($value instanceof SerializedCacheValue) {
$value = $this->serializer->deserialize($value->data, $value->type, 'json');
$event->setValue($value);
}
}
public function onAfterCacheUpdate(CacheUpdateEvent $event)
{
$cache = $event->getCache();
$key = $event->getKey();
$metadata = $event->getMetadata();
if (null !== $metadata->type) {
$serializedValue = $this->serializer->serialize($event->getValue(), 'json');
$value = new SerializedCacheValue($metadata->type, $serializedValue);
$this->cacheManager->getCache($cache)->set($key, $value);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.