Add cache abstraction and method annotations for controlling cache. The current implementation of the Cache component is a wrapper (proxy) for Doctrine\Common\Cache.
Add TTL strategy- Add annotation handling for overridden classes
- Write more tests
Use cache name (from config) as namespace for cache keys- Add cache configuration factories for all available cache drivers in Doctrine Cache
ArrayCacheApcCacheMemcachedCache- MemcacheCache
- FileCache
- RedisCache
- [WIP] Add @CacheUpdate annotation for updating cache entry after method execution
Add options to @CacheEvict annotation for deleting all entries from cache.- Add @CacheTTL annotation ??
- [WIP] Add DataCollector for Cache operations
First, install the bundle package with composer:
$ php composer.phar require kitano/cache-bundleNext, activate the bundle (and bundle it depends on) into app/AppKernel.php:
<?php// ...publicfunctionregisterBundles(){$bundles = array( //...newKitano\PelBundle\KitanoPelBundle(), newKitano\CacheBundle\KitanoCacheBundle(), ); // ... }services: my_manager.product: class: My\Manager\ProductManagertags: - {name: kitano_cache.cache_eligible }kitano_cache: annotations: {enabled: true }manager: simple_cachekey_generator: simple_hashmetadata: use_cache: true # Whether or not use metadata cachecache_dir: %kernel.cache_dir%/kitano_cachecache: products: type: memcachedservers: memcached-01: {host: localhost, port: 11211 }Note: The kitano_cache.cache_eligible tag is mandatory in your service definition if you want to be able to use annotation for this service.
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:
<?phpnamespaceMy\Manager; useKitano\CacheBundle\Annotation\Cacheable; class ProductManager{private$cacheManager; private$keyGenerator; publicfunction__construct(CacheManagerInterface$cacheManager, KeyGeneratorInterface$keyGenerator){$this->cacheManager = $cacheManager; $this->keyGenerator = $keyGenerator} publicfunctiongetProduct($sku, $type = 'book'){$cacheKey = $this->keyGenerator->generate($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} publicfunctionsaveProduct(Product$product){// saving product ...$cacheKey = $this->keyGenerator->generate($product->getSku()); $this->cacheManager->getCache('products')->delete($cacheKey)} }Out of the box, the bundle provides a SimpleCacheManager, but custom cache managers can be used instead of the default one and must implement the CacheManagerInterface.
Key generation is up to the developer, but for convenience, the bundle comes with some key generation logic.
Note: When using Annotation based caching, usage of Key generators is mandatory.
Out of the box, the bundle provides a SimpleHashKeyGenerator which basically adds each param encoded using md5 algorithm, and returned a md5 hash of the result.
For testing purpose you may also use LiteralKeyGenerator which build a slug-like key.
Note: Both generators does not support non-scalar keys such as objects.
You can override the Key Generator by setting the key_generator key in your config.yml
Allowed values are: simple_hash, literal or the id of the service of your custom Key generator
Custom key generators can be used instead of the default one and must implement the KeyGeneratorInterface.
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 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.
<?phpnamespaceMy\Manager; useMy\Model\Product; useKitano\CacheBundle\Annotation\Cacheable; class ProductManager{/** * @Cacheable(caches="products", key="#sku") */publicfunctiongetProduct($sku, $type = 'book'){$product = newProduct($sku, $type); return$product} }@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.
<?phpnamespaceMy\Manager; useMy\Model\Product; useKitano\CacheBundle\Annotation\CacheEvict; class ProductManager{/** * @CacheEvict(caches="products", key="#product.getSku()") */publicfunctionsaveProduct(Product$product){// saving product ... } }It is also possible to flush completely the caches by setting allEntries parameter to true
<?phpnamespaceMy\Manager; useMy\Model\Product; useKitano\CacheBundle\Annotation\CacheEvict; class ProductManager{/** * @CacheEvict(caches="products", allEntries=true) */publicfunctionsaveProduct(Product$product){// saving product ... } }Note: If you also provide a key, it will be ignored and the cache will be flushed.
@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.
<?phpnamespaceMy\Manager; useMy\Model\Product; useKitano\CacheBundle\Annotation\CacheUpdate; class ProductManager{/** * @CacheUpdate(caches="products", key="#product.getSku()") */publicfunctionsaveProduct(Product$product){// saving product....return$product} }For key generation, PHP Expression Language can be used.
TODO: write some doc here
Since this bundle provides a cache abstraction and not all cache providers support or handle TTL the same way, TTL strategy must be defined in each cache configuration options (when option is supported).
Example:
kitano_cache: annotations: {enabled: true }manager: simple_cachecache: products: type: memcachedttl: 86400# 1 dayservers: memcached-01: {host: localhost, port: 11211 }user_feeds: type: memcachedttl: 0# infinite (same as omitting the option)followers_list: type: apcttl: 1296000# 15 daysInstall development dependencies
$ composer install --devRun the test suite
$ vendor/bin/phpunitThis bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE 