For those who are awake!
Discount -15%
We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
New design
Control panel
  • Русский
  • Українська
  • English
  • UAH
  • USD
  • RUB
  • EUR
  • 0-800-307-307 Hotline
  • +38 (044) 392-74-33 Kiev
  • +38 (057) 728-39-00 Kharkiv
  • +38 (056) 794-38-31 Dnipro
  • +38 (032) 229-58-93 Lviv
  • +38 (048) 738-57-70 Odessa
  • +38(093) 170-15-42  Life
  • +38 (067) 400-88-44 Kievstar
  • +1(888)393-24-51  USA, Toll free
  • +44(131)507-01-14  Great Britain
  • +7 (499) 348-28-61 Moscow

2.13.4.19. Configuring Redis in OpenCart

To enable support Redis in OpenCart 3.0, do the following:
  1. At the end of each of the two config files — config.php and admin/config.php - add the following code (in the line with the parameter CACHE_HOSTNAME instead example substitute hosting account namewhere the site is located):
    // CACHE
    define('CACHE_HOSTNAME', '/home/example/.system/redis.sock');
    define('CACHE_PORT', 0);
    define('CACHE_PREFIX', 'oc_');
  2. In file system/library/cache/redis.php:
    1. Find a line like this:
      $this->cache->setTimeout(CACHE_PREFIX . $key, $this->expire);

      And replace in it setTimeout on expireto make it look like this:

      $this->cache->expire(CACHE_PREFIX . $key, $this->expire);
    2. Find a line like this:
      $this->cache->delete(CACHE_PREFIX . $key);

      And replace in it delete on delto make it look like this:

      $this->cache->del(CACHE_PREFIX . $key);
  3. 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 replace file on redisto make this block look like this:

    // Cache
    $_['cache_engine'] = 'redis'; // apc, file, mem or memcached
    $_['cache_expire'] = 3600;
  4. Check the site is working.
To enable support Redis in OpenCart 2.2-2.3, do the following:
  1. At the end of each of the two config files — config.php and admin/config.php - add the following code (in the line with the parameter CACHE_HOSTNAME instead example substitute hosting account namewhere the site is located):
    // CACHE
    define('CACHE_HOSTNAME', '/home/example/.system/redis.sock');
    define('CACHE_PORT', 0);
    define('CACHE_PREFIX', 'oc_');
  2. In the catalog system/library/cache create a file redis.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);
    	}
    }
  3. 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 replace file on redisto make this block look like this:

    // Cache
    $_['cache_type'] = 'redis'; // apc, file, mem or memcached
    $_['cache_expire'] = 3600;
  4. Check the site is working.
To enable support Redis in OpenCart 2.0-2.1, do the following:
  1. At the end of each of the two config files — config.php and admin/config.php - add the following code (in the line with the parameter CACHE_HOSTNAME instead example substitute hosting account namewhere the site is located):
    // CACHE
    define('CACHE_HOSTNAME', '/home/example/.system/redis.sock');
    define('CACHE_PORT', 0);
    define('CACHE_PREFIX', 'oc_');
  2. In the catalog system/library/cache create a file redis.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);
    	}
    }
  3. In each of the two index files - index.php and admin/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 replace file on redisto make this block look like this:

    // Cache
    $cache = new Cache('redis');
    $registry->set('cache', $cache);
  4. Check the site is working.
To enable support Redis in OpenCart 1.5, do the following:
  1. At the end of each of the two config files — config.php and admin/config.php - add the following code (in the line with the parameter REDIS_HOSTNAME instead example substitute hosting account namewhere the site is located):
    // CACHE
    define('CACHE_DRIVER', 'redis');
    define('REDIS_HOSTNAME', '/home/example/.system/redis.sock');
    define('REDIS_PORT', '0');
  2. Replace file content system/library/cache.php on such1):
    <?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();
                        }
                    }
                }
            }
        }
    }
  3. Check the site is working.

1)
Driver code taken from ofthis repository.