Cache in Oro Application¶
The OroCacheBundle
is responsible for operations with various kinds of caches.
Abstract Cache Services¶
There are two abstract services you can use as a parent for your cache services:
oro.file_cache.abstract
– this cache should be used for caching data private for each node in a web farmoro.cache.abstract
– this cache should be used for caching data that need to be shared between nodes in a web farm
The following example shows how this services can be used:
services:
acme.test.cache:
public: false
parent: oro.cache.abstract
calls:
- [ setNamespace, [ 'acme_test' ] ]
Also each of these abstract services can be re-declared in the application configuration file, for example:
services:
oro.cache.abstract:
abstract: true
class: Oro\Bundle\CacheBundle\Provider\PhpFileCache
arguments: [%kernel.cache_dir%/oro_data]
Caching Policy¶
Memory Based Cache¶
One of the most important thing when dealing with caches is proper cache invalidation. When using memory based cache we need to make sure that we do not keep old values in memory. Consider this example:
<?php
class LocalizationManager
{
/** @var \Doctrine\Common\Cache\ArrayCache */
private $cacheProvider;
public function getLocalization($id)
{
$localization = $this->cacheProvider->fetch($id);
// ... all other operations, fetch from DB if cache is empty
// ... save in cache data from DB
return $localization;
}
}
Since $cacheProvider
in our example is an implementation of memory
ArrayCache
, we will keep the data there until the process ends. With
HTTP request this would work perfectly well, but when our
LocalizationManager
would be used in some long-running cli
processes, we have to manually clear memory cache after every change
with Localizations. Missing cache clearing for any of these cases leads
to the outdated data in LocalizationManager
.
Cache Chaining¶
Solution to the issue mentioned above is to keep a healthy balance
between the fast and shared cache. It is implemented in the
ChainCache
class.
<?php
namespace Oro\Bundle\CacheBundle\Provider;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\ChainCache;
class MemoryCacheChain extends ChainCache
{
/**
* {@inheritdoc}
*/
public function __construct($cacheProviders = [])
{
if (PHP_SAPI !== 'cli') {
array_unshift($cacheProviders, new ArrayCache());
}
parent::__construct($cacheProviders);
}
}
This class checks whether a request comes from the CLI. If not, the
memory ArrayCache
is added to the top of the cache providers which
are being used for caching. With these priorities set, all HTTP requests
gain performance when dealing with caches in memory and the CLI
processes have no issues with the outdated data as they use the
persistent cache.
Default Cache Implementation¶
There are two abstract services you can use
as a parent for your cache services. Default implementations are
following: – for CLI requests: MemoryCacheChain
with only
Oro\Bundle\CacheBundle\Provider\FilesystemCache
as a cache provider
– for other requests: MemoryCacheChain
with ArrayCache
on the
top of FilesystemCache
APC Cache¶
There is a possibility to use APC cache and few steps should be completed for this.
First of all, APC should be installed and enabled in the system. After
this, the production configuration file (config_prod.yml
) should be
updated with the following parameters:
doctrine:
orm:
auto_mapping: true
query_cache_driver: apc
metadata_cache_driver: apc
result_cache_driver: apc
services:
oro.cache.abstract:
abstract: true
class: Doctrine\Common\Cache\ApcCache
On the last step of the configuration, production cache should be cleared.
Caching of Symfony Validation Rules¶
By default, rules for Symfony Validation Component are cached using
oro.cache.abstract
service, but you can change this to make
validation caching suit some custom requirements. To do this, you need
to redefine oro_cache.provider.validation
service.