With symfony 3.1 we got Cache Component (https://github.com/symfony/cache)
I can't find any documentation or examples for this component because it's new.
Can anybody write simple example how to use this component with symfony 3.1
To clear the cache you can use the bin/console cache:pool:clear [pool] command. That will remove all the entries from your storage and you will have to recalculate all the values. You can also group your pools into "cache clearers".
The Cache component provides features covering simple to advanced caching needs. It natively implements PSR-6 and the Cache Contracts for greatest interoperability. It is designed for performance and resiliency, ships with ready to use adapters for the most common caching backends.
In an enterprise server, a Caching SAN Adapter is a host bus adapter (HBA) for storage area network (SAN) connectivity which accelerates performance by transparently storing duplicate data such that future requests for that data can be serviced faster compared to retrieving the data from the source.
The cache component is mainly used internal in Symfony for Serializers etc.
But the latest FrameworkBundle already supports creating your own cache pools via config.yml. There doesnt seem to be any docs on this at the moment, so i digged myself through:
In config.yml you can create f.e. a new cache
framework:
...
cache:
default_redis_provider: redis://%cache.redis_host%:%cache.redis_port%/%cache:redis_db%
pools:
my_cache:
adapter: cache.adapter.redis
public: true
default_lifetime: 1200
provider: cache.default_redis_provider
Of course you can also just make your own service defintion.
In your code you can then use the created cache pool to create CacheItems and cache them:
$cacheItem = $this->get('my_cache')->getItem($cacheKey = $item->getId());
if(!$cacheItem->isHit()){
$cacheItem->set($item);
$cacheItem->expiresAfter(null); //this needs to be called to use defaultTime
$this->get('my_cache')->save($cacheItem);
}
The Psr-6 CacheItem gets created by the pool if it not exists in the cache.
It will get the key that it first got queried with. Then you can set a value and expiration time and save it to the cache.
For more infos on what and how you can do with the PSR-6 cache see here: http://www.php-fig.org/psr/psr-6/
The symfony docs for the component (note: only for the component, not for the framework integration) are still a PR but you can precheck it here: https://github.com/symfony/symfony-docs/pull/6515
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With