Gérer les cookies utilisés pour la publicité, tels que la personnalisation des annonces, le remarketing et l’analyse des performances des annonces.
2.14.3.19. Configure Redis in OpenCart
Redis configuration process differs in different OpenCart versions.
- At the end of each of two configuration files
config.phpandadmin/config.phpadd this code (in line withCACHE_HOSTNAMEparameter, instead ofexampleinsert hosting account name where site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - In
system/library/cache/redis.phpfile:- Find this line:
$this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire);And replace
setTimeoutwithexpirein it so that it looks as follows:$this->cache->expire(CACHE_PREFIX . $key, $this->expire); - Find this line:
$this->cache->delete(CACHE_PREFIX . $key);And replace
deletewithdelin it so that it looks like this:$this->cache->del(CACHE_PREFIX . $key);
- In
system/config/default.phpfile, find this code block:// Cache $_['cache_engine'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;And in line with
cache_engineparameter, replacefilewithredisso that this block looks as follows:// Cache $_['cache_engine'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600; - Check the site operation.
- At the end of each of two configuration files
config.phpandadmin/config.phpadd this code (in line withCACHE_HOSTNAMEparameter, instead ofexampleinsert hosting account name where site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - In
system/library/cachedirectory, createredis.phpfile with this content:<?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); } } - In
system/config/default.phpfile, find this code block:// Cache $_['cache_type'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;And in line with
cache_engineparameter, replacefilewithredisso that this block looks as follows:// Cache $_['cache_type'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600; - Check the site operation.
- At the end of each of two configuration files
config.phpandadmin/config.phpadd this code (in line withCACHE_HOSTNAMEparameter, instead ofexampleinsert hosting account name where site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_'); - In
system/library/cachedirectory, createredis.phpfile with this content:<?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); } } - In each of two index files —
index.phpandadmin/index.php— find this code block:// Cache $cache = new Cache('file'); $registry->set('cache', $cache);And in the line with
$cacheparameter, replacefilewithredisso that this block looks as follows:// Cache $cache = new Cache('redis'); $registry->set('cache', $cache); - Check the site operation.
- At the end of each of two configuration files
config.phpandadmin/config.phpadd this code (in line withREDIS_HOSTNAMEparameter, replaceexamplewith hosting account name where site is hosted):// CACHE define('CACHE_DRIVER', 'redis'); define('REDIS_HOSTNAME', '/home/example/.system/redis.sock'); define('REDIS_PORT', '0'); - Replace contents of
system/library/cache.phpfile with the following (driver code taken from here):<?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(); } } } } } } - Check the site operation.
(2)