Керування файлами cookie, які використовуються для реклами, таких як персоналізація реклами, ремаркетинг і аналіз ефективності реклами.
2.14.3.19. Налаштування Redis в OpenCart
Процес налаштування Redis відрізняється в різних версіях OpenCart.
- В кінці кожного з двох конфігураційних файлів
config.phpіadmin/config.phpдодайте такий код (у рядку з параметромCACHE_HOSTNAMEзамістьexampleпідставте назву хостинг-акаунта, в якому розміщений сайт):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - У файлі
system/library/cache/redis.php:- Знайдіть такий рядок:
$this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire);І замініть в ньому
setTimeoutнаexpire, щоб він мав такий вигляд:$this->cache->expire(CACHE_PREFIX . $key, $this->expire); - Знайдіть такий рядок:
$this->cache->delete(CACHE_PREFIX . $key);І замініть в ньому
deleteнаdel, щоб він мав такий вигляд:$this->cache->del(CACHE_PREFIX . $key);
- У файлі
system/config/default.phpзнайдіть такий блок коду:// Cache $_['cache_engine'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;І в рядку з параметром
cache_engineзамінітьfileнаredis, щоб цей блок мав такий вигляд:// Cache $_['cache_engine'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600; - Перевірте роботу сайту.
- В кінці кожного з двох конфігураційних файлів
config.phpіadmin/config.phpдодайте такий код (у рядку з параметромCACHE_HOSTNAMEзамістьexampleпідставте назву хостинг-акаунта, в якому розміщений сайт):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - У каталозі
system/library/cacheстворіть файлredis.phpз таким вмістом:<?php namespace Cache; class Redis { private $expire; private $cache; public function __construct($expire) { $this->expire = $expire; $this->cache = new \Redis(); $this->cache->pconnect(CACHE_HOSTNAME, CACHE_PORT); } public function get($key) { $data = $this->cache->get(CACHE_PREFIX . $key); return json_decode($data, true); } public function set($key, $value) { $status = $this->cache->set(CACHE_PREFIX . $key, json_encode($value)); if ($status) { $this->cache->expire(CACHE_PREFIX . $key, $this->expire); } return $status; } public function delete($key) { $this->cache->delete(CACHE_PREFIX . $key); } } - У файлі
system/config/default.phpзнайдіть такий блок коду:// Cache $_['cache_type'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;І в рядку з параметром
cache_engineзамінітьfileнаredis, щоб цей блок мав такий вигляд:// Cache $_['cache_type'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600; - Перевірте роботу сайту.
- В кінці кожного з двох конфігураційних файлів
config.phpіadmin/config.phpдодайте такий код (у рядку з параметромCACHE_HOSTNAMEзамістьexampleпідставте назву хостинг-акаунта, в якому розміщений сайт):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - У каталозі
system/library/cacheстворіть файлredis.phpз таким вмістом:<?php namespace Cache; class Redis { private $expire; private $cache; public function __construct($expire) { $this->expire = $expire; $this->cache = new \Redis(); $this->cache->pconnect(CACHE_HOSTNAME, CACHE_PORT); } public function get($key) { $data = $this->cache->get(CACHE_PREFIX . $key); return json_decode($data, true); } public function set($key, $value) { $status = $this->cache->set(CACHE_PREFIX . $key, json_encode($value)); if ($status) { $this->cache->expire(CACHE_PREFIX . $key, $this->expire); } return $status; } public function delete($key) { $this->cache->delete(CACHE_PREFIX . $key); } } - У кожному з двох індексних файлів —
index.phpіadmin/index.php— знайдіть такий блок коду:// Cache $cache = new Cache('file'); $registry->set('cache', $cache);І в рядку з параметром
$cacheзамінітьfileнаredis, щоб цей блок мав такий вигляд:// Cache $cache = new Cache('redis'); $registry->set('cache', $cache); - Перевірте роботу сайту.
- В кінці кожного з двох конфігураційних файлів
config.phpіadmin/config.phpдодайте такий код (у рядку з параметромREDIS_HOSTNAMEзамістьexampleпідставте назву хостинг-акаунта, в якому розміщений сайт):// CACHE define('CACHE_DRIVER', 'redis'); define('REDIS_HOSTNAME', '/home/example/.system/redis.sock'); define('REDIS_PORT', '0'); - Замініть вміст файлу
system/library/cache.phpна такий (код драйвера взято звідси):<?php final class Cache { private $expire = 3600; private $serialize_type = 'json'; private $cache_mode = 'redis'; public function __construct() { $this->site_key = substr(md5(HTTP_SERVER), 0, 5); $this->redis = new Redis(); if (!$this->redis->connect(REDIS_HOSTNAME, REDIS_PORT)) { $this->cache_mode = 'file'; } } public function getCacheMode() { return $this->cache_mode; } public function setCacheMode($cache_mode = 'redis') { $this->cache_mode = $cache_mode; } public function setSerializeType($serialize_type = 'serialize') { $this->serialize_type = $serialize_type; } public function getSerializeType() { return $this->serialize_type; } public function get($key) { if ($this->cache_mode == 'redis') { $cache = $this->redis->get($key . '.' . $this->site_key); } else { $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*'); if ($files) { $cache = file_get_contents($files[0]); foreach ($files as $file) { $time = substr(strrchr($file, '.'), 1); if ($time < time()) { if (file_exists($file)) { unlink($file); } } } } } if ($this->serialize_type == 'json') { $data = json_decode($cache, true); } else { $data = unserialize($cache); } return $data; } public function set($key, $value, $expire = 0) { if ($expire == 0) { $expire = rand($this->expire, $this->expire + 400); } if ($this->cache_mode == 'redis') { $res = $this->redis->set($key . '.' . $this->site_key, json_encode($value), $expire); } else { $this->remove($key); $file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $expire); $handle = fopen($file, 'w'); if ($this->serialize_type == 'json') { $data = json_encode($value); } else { $data = serialize($value); } fwrite($handle, $data); fclose($handle); } } public function delete($key) { if ($this->cache_mode == 'redis') { $this->redis->delete($key . '.' . $this->site_key); } else { $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*'); if ($files) { foreach ($files as $file) { if (file_exists($file)) { unlink($file); clearstatcache(); } } } } } } - Перевірте роботу сайту.
(2)