SimpleCacheBundle
It provide a data caching for many ressources. It can be easly extended with an other storage method., (*1)
Nick: aways
IRC: irc.freenode.net - #symfony-fr, (*2)
Install
- Download with composer
- Enable the Bundle
- Configure
- Use the bundle
- Example
Composer
Add SimpleCacheBundle in your project's composer.json
, (*3)
{
"require": {
"imag/simple-cache-bundle": "dev-master"
}
}
Enable the Bundle
``` php
<?php
// app/AppKernel.php, (*4)
public function registerBundles()
{
$bundles = array(
// ...
new IMAG\LdapBundle\IMAGSimpleCacheBundle(),
);
}, (*5)
### Configure
``` yml
#config.yml
imag_simple_cache:
storage_method: hdd
storage:
hdd:
class: \IMAG\SimpleCacheBundle\Storage\HddStorage
default_lifetime: 3600
extras:
base_dir: /tmp
cache_folder: cache_q1hU5HHt
file_extension: imag.cache
deep: 3
folder_length: 3
Console
``` bash
php app/console help simplecache:cache-clear
Usage:
simplecache:cache-clear [--entire-cache], (*6)
By default, clear-cache without any parameter clear expired references only.
### Use the bundle
#### Inject:
``` xml
<service id="foo_bar.service" class="%foo_bar.service.class%">
<argument type="service" id="imag_simple_cache.cache_manager" />
</service>
Methods
mixed getReference(mixed $param)
this addReference(mixed $param)
this setReferenceKey(string $key)
string getReferenceKey()
array clearCache()
array clearExpired()
Note:, (*7)
If you don't use setReferenceKey($param) method the storage reference key is calculate with a simple process:
md5(serialize($param)), (*8)
Example
``` php
public function __construct(\IMAG\SimpleCacheBundle\Manager\CacheManager $cache)
{
$this->cache = $cache;
}, (*9)
public function getArchiveOfAppsPdf(array $applications)
{
$key = md5(serialize($applications));, (*10)
$this->cache->setReferenceKey($key);
// Because setReference have been called, this reference is searched with the key $key
if ($cached = $this->cache->getReference()) {
return $cached;
}
$zip = $this->zip->getZip();
// Because setReference have been called, this method use $key to store the $zip reference
$this->cache->addReference($zip);
return $zip;
}, (*11)
public function getExample(array $applications)
{
$zip = $this->getZip($applications);, (*12)
// This reference is searched under md5(serialize($zip))
if ($cached = $this->cache->getReference($zip)) {
return $cached;
}
// This reference is stored under md5(serialize($zip))
$this->cache->addReference($zip);
return $zip;
}
```, (*13)