Download the PHP package tbbc/cache-bundle without Composer
On this page you can find all versions of the php package tbbc/cache-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tbbc/cache-bundle
More information about tbbc/cache-bundle
Files in tbbc/cache-bundle
Package cache-bundle
Short Description Symfony Bundle - Cache abstraction and method annotations for controlling cache
License MIT
Informations about the package cache-bundle
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.
Features
@Cacheable
,@CacheUpdate
,@CacheEvict
annotation 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
- Annotation based caching (recommanded)
- @Cacheable annotation
- @CacheEvict annotation
- @CacheUpdate annotation
- Expression Language
- Standard cache usage (without annotations)
- Custom Cache Manager
- Key generation
- Custom Key generation
- TTL Strategy
- Annotation based caching (recommanded)
- Symfony debug toolbar integration
- Known limitations
- Testing
- License
Installation
First, install the bundle package with composer:
Next, activate the bundle into app/AppKernel.php
:
Configuration
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
- @CacheEvict
- @CacheUpdate
@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.
@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.
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.
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.
Expression Language
For key generation, Symfony Expression Language can be used.
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:
Custom Cache Manager
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
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 generation
Custom key generators can be used instead of the default one and must implement the KeyGeneratorInterface.
TTL Strategy
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:
Symfony debug toolbar integration
Debugging cache operations is often a pain is the ass. In order to facilitate this work, the bundle adds some useful live information directly in the Symfony Debug Toolbar.
Here are some screenshots about the kind of information it will show:
Known limitations
CacheEvict operation with "allEntries" option
Due to the way cache is managed in Doctrine and especially the way it is handled by the different cache systems, usage of the "CacheEvict" operation with the "allEntries" option can lead to undesired behaviour.
Be warned that if you use different cache namespaces but within the same cache instance (like a single memcached server for instance), the "allEntries" option will flush all cache entries in all namespaces. Meaning it will flush the entire instance cache.
Doctrine entities caching
Automatic caching of doctrine entities is not supported at this time. If you need to cache entities, you have to implement your own logic.
One way to do it would be to override annotations and metadatas for adding a serialization "type" option, and then hook in cache events to manually manage serialization/de-serialization operations:
Testing
Install development dependencies
Run the test suite
License
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE
All versions of cache-bundle with dependencies
doctrine/cache Version >=1.0
jms/metadata Version 1.*
jms/aop-bundle Version >=1.0.0,<1.2-dev
symfony/expression-language Version ~2.4