33 private $_objectManager;
45 private $_enforcedOptions = [];
56 private $_decorators = [];
71 'hashed_directory_level' => 1,
72 'file_name_prefix' =>
'mage',
93 array $enforcedOptions = [],
94 array $decorators = []
99 $this->_enforcedOptions = $enforcedOptions;
100 $this->_decorators = $decorators;
109 public function create(array
$options)
113 foreach ([
'backend_options',
'slow_backend_options'] as $section) {
114 if (!empty(
$options[$section][
'cache_dir'])) {
116 $directory->create(
$options[$section][
'cache_dir']);
117 $options[$section][
'cache_dir'] = $directory->getAbsolutePath(
$options[$section][
'cache_dir']);
122 if (!$idPrefix && isset(
$options[
'prefix'])) {
125 if (empty($idPrefix)) {
128 substr(md5($configDirPath), 0, 3) .
'_';
130 $options[
'frontend_options'][
'cache_id_prefix'] = $idPrefix;
138 'operation' =>
'cache:create',
139 'frontend_type' => $frontend[
'type'],
140 'backend_type' => $backend[
'type'],
142 \Magento\Framework\Profiler::start(
'cache_frontend_create', $profilerTags);
145 $result = $this->_objectManager->create(
146 \
Magento\Framework\Cache\Frontend\Adapter\Zend::class,
148 'frontendFactory' =>
function () use ($frontend, $backend) {
149 return \Zend_Cache::factory(
164 \Magento\Framework\Profiler::stop(
'cache_frontend_create');
174 private function _getExpandedOptions(array
$options)
176 return array_replace_recursive(
$options, $this->_enforcedOptions);
187 private function _applyDecorators(\
Magento\Framework\Cache\FrontendInterface $frontend)
189 foreach ($this->_decorators as $decoratorConfig) {
190 if (!isset($decoratorConfig[
'class'])) {
191 throw new \LogicException(
'Class has to be specified for a cache frontend decorator.');
193 $decoratorClass = $decoratorConfig[
'class'];
194 $decoratorParams = isset($decoratorConfig[
'parameters']) ? $decoratorConfig[
'parameters'] : [];
195 $decoratorParams[
'frontend'] = $frontend;
197 $frontend = $this->_objectManager->create($decoratorClass, $decoratorParams);
198 if (!$frontend instanceof \
Magento\Framework\Cache\FrontendInterface) {
199 throw new \UnexpectedValueException(
'Decorator has to implement the cache frontend interface.');
215 $enableTwoLevels =
false;
217 if (isset($cacheOptions[
'backend_options']) && is_array($cacheOptions[
'backend_options'])) {
218 $options = $cacheOptions[
'backend_options'];
223 $backendType =
false;
224 switch (strtolower(
$type)) {
226 if (extension_loaded(
'sqlite') && isset(
$options[
'cache_db_complete_path'])) {
227 $backendType =
'Sqlite';
231 if (extension_loaded(
'memcached')) {
232 if (isset($cacheOptions[
'memcached'])) {
233 $options = $cacheOptions[
'memcached'];
235 $enableTwoLevels =
true;
236 $backendType =
'Libmemcached';
237 }
elseif (extension_loaded(
'memcache')) {
238 if (isset($cacheOptions[
'memcached'])) {
239 $options = $cacheOptions[
'memcached'];
241 $enableTwoLevels =
true;
242 $backendType =
'Memcached';
246 if (extension_loaded(
'apc') &&
ini_get(
'apc.enabled')) {
247 $enableTwoLevels =
true;
248 $backendType =
'Apc';
252 if (extension_loaded(
'xcache')) {
253 $enableTwoLevels =
true;
254 $backendType =
'Xcache';
258 case 'varien_cache_backend_eaccelerator':
259 if (extension_loaded(
'eaccelerator') &&
ini_get(
'eaccelerator.enable')) {
260 $enableTwoLevels =
true;
261 $backendType = \Magento\Framework\Cache\Backend\Eaccelerator::class;
265 $backendType = \Magento\Framework\Cache\Backend\Database::class;
268 case 'remote_synchronized_cache':
269 $backendType = \Magento\Framework\Cache\Backend\RemoteSynchronizedCache::class;
270 $options[
'remote_backend'] = \Magento\Framework\Cache\Backend\Database::class;
272 $options[
'local_backend'] = \Cm_Cache_Backend_File::class;
274 $options[
'local_backend_options'][
'cache_dir'] = $cacheDir->getAbsolutePath();
278 if (
$type != $this->_defaultBackend) {
281 $implements = class_implements(
$type,
true);
282 if (in_array(
'Zend_Cache_Backend_Interface', $implements)) {
283 $backendType =
$type;
286 }
catch (\Exception $e) {
293 $this->_backendOptions[
'cache_dir'] = $cacheDir->getAbsolutePath();
302 $backendOptions = [
'type' => $backendType,
'options' =>
$options];
303 if ($enableTwoLevels) {
306 return $backendOptions;
316 $options[
'adapter_callback'] =
function () {
317 return $this->_resource->getConnection();
319 $options[
'data_table_callback'] =
function () {
320 return $this->_resource->getTableName(
'cache');
322 $options[
'tags_table_callback'] =
function () {
323 return $this->_resource->getTableName(
'cache_tag');
338 $options[
'fast_backend'] = $fastOptions[
'type'];
339 $options[
'fast_backend_options'] = $fastOptions[
'options'];
340 $options[
'fast_backend_custom_naming'] =
true;
341 $options[
'fast_backend_autoload'] =
true;
342 $options[
'slow_backend_custom_naming'] =
true;
343 $options[
'slow_backend_autoload'] =
true;
345 if (isset($cacheOptions[
'auto_refresh_fast_cache'])) {
346 $options[
'auto_refresh_fast_cache'] = (bool)$cacheOptions[
'auto_refresh_fast_cache'];
348 $options[
'auto_refresh_fast_cache'] =
false;
350 if (isset($cacheOptions[
'slow_backend'])) {
351 $options[
'slow_backend'] = $cacheOptions[
'slow_backend'];
355 if (isset($cacheOptions[
'slow_backend_options'])) {
356 $options[
'slow_backend_options'] = $cacheOptions[
'slow_backend_options'];
360 if (
$options[
'slow_backend'] ==
'database') {
361 $options[
'slow_backend'] = \Magento\Framework\Cache\Backend\Database::class;
363 if (isset($cacheOptions[
'slow_backend_store_data'])) {
364 $options[
'slow_backend_options'][
'store_data'] = (bool)$cacheOptions[
'slow_backend_store_data'];
366 $options[
'slow_backend_options'][
'store_data'] =
false;
370 $backend = [
'type' =>
'TwoLevels',
'options' =>
$options];
383 $options = isset($cacheOptions[
'frontend_options']) ? $cacheOptions[
'frontend_options'] : [];
384 if (!array_key_exists(
'caching',
$options)) {
387 if (!array_key_exists(
'lifetime',
$options)) {
389 $cacheOptions[
'lifetime']
392 if (!array_key_exists(
'automatic_cleaning_factor',
$options)) {
393 $options[
'automatic_cleaning_factor'] = 0;
396 isset($cacheOptions[
'frontend']) ? $cacheOptions[
'frontend'] : \Magento\Framework\Cache\Core::class;
elseif(isset( $params[ 'redirect_parent']))
const PARAM_CACHE_FORCED_OPTIONS
_getBackendOptions(array $cacheOptions)
_getFrontendOptions(array $cacheOptions)
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, Filesystem $filesystem, \Magento\Framework\App\ResourceConnection $resource, array $enforcedOptions=[], array $decorators=[])
_getTwoLevelsBackendOptions($fastOptions, $cacheOptions)