TbbcCacheBundle
Cache abstraction bundle for Symfony 2
Install / Use
npx skills add TheBigBrainsCompany/TbbcCacheBundleInstalls into whichever agent you are using.
README
CacheBundle
Add cache abstraction and method annotations for controlling cache. The current implementation of the Cache component is a wrapper (proxy) for Doctrine\Common\Cache.
State
Overview
The TbbcCacheBundle integrates Symfony with a non-instrusive applicative cache management system. It gives to the developer annotation driven cache control by using AOP mechanisms and PHP language expressions.
<?php
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);
}
}
Features
@Cacheable,@CacheUpdate,@CacheEvictannotation support- TTL strategy, allow you to customize cache retention
- Namespaced cache manager
- Multiple cache managers:
- Doctrine/ArrayCache
- Doctrine/ApcCache
- Doctrine/MemcachedCache
- Doctrine/RedisCache
- Symfony Debug Toolbar integration
Documentation
- Installation
- Configuration
- Usage
- Symfony debug toolbar integration
- Known limitations
- Testing
- License
Installation
First, install the bundle package with composer:
$ php composer.phar require tbbc/cache-bundle
Next, activate the bundle into app/AppKernel.php:
<?php
// ...
public function registerBundles()
{
$bundles = array(
//...
new Tbbc\CacheBundle\TbbcCacheBundle(),
);
// ...
}
Configuration
services:
my_manager.product:
class: My\Manager\ProductManager
tags:
- { name: tbbc_cache.cache_eligible }
tbbc_cache:
annotations: { enabled: true }
manager: simple_cache
key_generator: simple_hash
metadata:
use_cache: true # Whether or not use metadata cache
cache_dir: %kernel.cache_dir%/tbbc_cache
cache:
products:
type: memcached
servers:
memcached-01: { host: localhost, port: 11211 }
Note: The tbbc_cache.cache_eligible tag is mandatory in your service definition if you want to be able to use
annotation for this service.
Usage
Annotation based caching (recommanded)
Recommended
If some prefer to avoid repeating code each time they want to add some caching logic, the bundle can automate the process by using AOP approach and annotations.
The bundle provides the following annotations:
@Cacheable annotation
@Cacheable annotation is used to automatically store the result of a method into the cache.
When a method demarcated with the @Cacheable annotation is called, the bundle checks if an entry exists in the cache before executing the method. If it finds one, the cache result is returned without having to actually execute the method.
If no cache entry is found, the method is executed and the bundle automatically stores its result into the cache.
<?php
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;
}
}
@CacheEvict annotation
@CacheEvict annotation allows methods to trigger cache population or cache eviction.
When a method is demarcated with @CacheEvict annotation, the bundle will execute the method and then will automatically try to delete the cache entry with the provided key.
<?php
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 ...
}
}
It is also possible to flush completely the caches by setting allEntries parameter to true
:warning: Important note: when using the allEntries option you have to be really careful, if you
use the same cache manager for different namespace, the whole cache manager will be flushed. This is currently
a limitation of the underlying Doctrine Cache library.
<?php
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 ...
}
}
Note: If you also provide a key, it will be ignored and the cache will be flushed.
@CacheUpdate annotation
@CacheUpdate annotation is useful for cases where the cache needs to be updated without interfering with the method execution.
When a method is demarcated with @CacheUpdate annotation, the bundle will always execute the method and then will automatically try to update the cache entry with the method result.
<?php
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;
}
}
Expression Language
For key generation, Symfony Expression Language can be used.
/**
* @CacheUpdate(caches="products", key="product.getSku()")
*/
public function updateProduct(Product $product)
{
// do something
}
The Expression Language allow you to retrieve any arguments passed to your method and use it to generate the cache key.
Standard cache usage (without annotations)
CacheManager instance must be injected into services that need cache management.
The CacheManager gives access to each configured cache (see Configuration section).
Each cache implements CacheInterface.
Usage:
<?php
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);
}
}
Custom Cache Manager
Out of the box, the bundle provides a [SimpleCacheManager](https://git
Related Skills
node-connect
385.5kDiagnose OpenClaw Android, iOS, or macOS node pairing, QR/setup code, route, auth, and connection failures.
blender-python-addon
40.5kBlender Python add-on rules for operators, panels, properties, registration, testing, and API-safe scripting
flutter-development-guidelines-cursorrules-prompt-file
40.5kCursor rules for Flutter development with MVVM architecture, Riverpod state management, Material widgets, and Dart style guidelines.
commit-push-pr
140.7kCommit, push, and open a PR




