2.13.4.19. Configuring Redis in OpenCart
Setting process Redis differs in different versions of OpenCart.
- At the end of each of the two configuration files —
config.php
andadmin/config.php
— add the following code (in the line with the parameterCACHE_HOSTNAME
insteadofexample
substitute hosting account namewhere the site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_');
- In file
system/library/cache/redis.php
:- Find a line like this:
$this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire);
And replace in it
setTimeout
onexpire
so that it looks like this:$this->cache->expire(CACHE_PREFIX . $key, $this->expire);
- Find a line like this:
$this->cache->delete(CACHE_PREFIX . $key);
And replace in it
delete
ondel
so that it looks like this:$this->cache->del(CACHE_PREFIX . $key);
- In file
system/config/default.php
find a block of code like this:// Cache $_['cache_engine'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;
And in the line with the parameter
cache_engine
replacefile
onredis
to make this block look like this:// Cache $_['cache_engine'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600;
- Check the site is working.
- At the end of each of the two configuration files —
config.php
andadmin/config.php
— add the following code (in the line with the parameterCACHE_HOSTNAME
insteadofexample
substitute hosting account namewhere the site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_');
- In the catalog
system/library/cache
create a fileredis.php
with content like this:<?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 file
system/config/default.php
find a block of code like this:// Cache $_['cache_type'] = 'file'; // apc, file, mem or memcached $_['cache_expire'] = 3600;
And in the line with the parameter
cache_engine
replacefile
onredis
to make this block look like this:// Cache $_['cache_type'] = 'redis'; // apc, file, mem or memcached $_['cache_expire'] = 3600;
- Check the site is working.
- At the end of each of the two configuration files —
config.php
andadmin/config.php
— add the following code (in the line with the parameterCACHE_HOSTNAME
insteadofexample
substitute hosting account namewhere the site is hosted):// CACHE define('CACHE_HOSTNAME', '/home/example/.system/redis.sock'); define('CACHE_PORT', 0); define('CACHE_PREFIX', 'oc_');
- In the catalog
system/library/cache
create a fileredis.php
with content like this:<?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 the two index files -
index.php
andadmin/index.php
— find a block of code like this:// Cache $cache = new Cache('file'); $registry->set('cache', $cache);
And in the line with the parameter
$cache
replacefile
onredis
to make this block look like this:// Cache $cache = new Cache('redis'); $registry->set('cache', $cache);
- Check the site is working.
- At the end of each of the two configuration files —
config.php
andadmin/config.php
— add the following code (in the line with the parameterREDIS_HOSTNAME
insteadofexample
substitute hosting account namewhere the site is hosted):// CACHE define('CACHE_DRIVER', 'redis'); define('REDIS_HOSTNAME', '/home/example/.system/redis.sock'); define('REDIS_PORT', '0');
- Replace file content
system/library/cache.php
to this (the driver code is taken fromhere):<?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 is working.