We use cookies
We use cookies to optimize our website. By continuing to browse the site, you agree to our use of cookies.
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.9. Memcache (d) setup in OpenCart

Memcache (d) setup is different for each OpenCart version. To follow the correct instructions, click on the corresponding version.

To enable support Memcached in OpenCart 3.0, do the following:
  1. At the end of each of the two config files add code like this:
    // CACHE
    define('CACHE_HOSTNAME', '/home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    In the line with the parameter CACHE_HOSTNAME instead example indicate hosting account namein which the site is hosted.

  2. Edit the file default.phplocated in the directory system/config:
    • Find a block of code like this:
      // Cache
      $_['cache_engine'] = 'file'; // apc, file, mem or memcached
      $_['cache_expire'] = 3600;
    • In the line with the parameter cache_engine replace file on memcachedso that the block of code looks like this:
      // Cache
      $_['cache_engine'] = 'memcached'; // apc, file, mem or memcached
      $_['cache_expire'] = 3600;
  3. Check the site is working.
To enable support Memcache in OpenCart 2.2-2.3, do the following:
  1. At the end of each of the two config files add code like this:
    // CACHE
    define('CACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    In the line with the parameter CACHE_HOSTNAME instead example indicate hosting account namein which the site is hosted.

  2. Edit the file default.phplocated in the directory system/config:
    • Find a block of code like this:
      // Cache
      $_['cache_type'] = 'file'; // apc, file or mem
      $_['cache_expire'] = 3600;
    • In the line with the parameter cache_type replace file on memso that the block of code looks like this:
      // Cache
      $_['cache_type'] = 'mem'; // apc, file or mem
      $_['cache_expire'] = 3600;
  3. Check the site is working.
To enable support Memcache in OpenCart 2.0-2.1, do the following:
  1. At the end of each of the two config files add code like this:
    // CACHE
    define('CACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    In the line with the parameter CACHE_HOSTNAME instead example indicate hosting account namein which the site is hosted.

  2. Edit two files index.phpone of which is in root directory site, the second - in the subdirectory admin:
    • Find a block of code like this:
      // Cache
      $cache = new Cache('file');
      $registry->set('cache', $cache);
    • On a line with a variable $cache replace file on memso that the block of code looks like this:
      // Cache
      $cache = new Cache('mem');
      $registry->set('cache', $cache);
  3. Check the site is working.
To enable support Memcache in OpenCart 1.5, do the following:
  1. At the end of each of the two config files add code like this:
    define('CACHE_DRIVER', 'memcached');
    define('MEMCACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('MEMCACHE_PORT', '0');
    define('MEMCACHE_NAMESPACE', 'opencart_test');

    In the line with the parameter MEMCACHE_HOSTNAME instead example indicate hosting account namein which the site is hosted.

  2. Replace the file system/library/cache.php on that:
    cache.php
    <?php
    final class Cache {
        private $expire;
        private $memcache;
        private $ismemcache = false;
        public function __construct($exp = 3600) {
            $this->expire = $exp;
            if (CACHE_DRIVER == 'memcached') {
                $mc = new Memcache;
                if ($mc->pconnect(MEMCACHE_HOSTNAME, MEMCACHE_PORT)) {
                    $this->memcache = $mc;
                    $this->ismemcache = true;
                };
            };
            $files = glob(DIR_CACHE . 'cache.*');
            if ($files) {
                foreach ($files as $file) {
                    $time = substr(strrchr($file, '.'), 1);
                    if ($time < time()) {
                        if (file_exists($file)) {
                            @unlink($file);
                        }
                    }
                }
            }
        }
        public function get($key) {
            if ((CACHE_DRIVER == 'memcached') && $this->ismemcache) {
                return($this->memcache->get(MEMCACHE_NAMESPACE . $key, 0));
            } else {
                $files = glob(DIR_CACHE . 'cache.' . $key . '.*');
                if ($files) {
                    foreach ($files as $file) {
                        $cache = '';
                        $handle = fopen($file, 'r');
                        if ($handle) {
                            $cache = fread($handle, filesize($file));
                            fclose($handle);
                        }
                        return unserialize($cache);
                    }
                }
            }
        }
        public function set($key, $value) {
            if ((CACHE_DRIVER == 'memcached') && $this->ismemcache) {
                $this->memcache->set(MEMCACHE_NAMESPACE . $key, $value, 0, $this->expire);
            } else {
                $this->delete($key);
                $file = DIR_CACHE . 'cache.' . $key . '.' . (time() + $this->expire);
                $handle = fopen($file, 'w');
                fwrite($handle, serialize($value));
                fclose($handle);
            };
        }
        public function delete($key) {
            if ((CACHE_DRIVER == 'memcached') && $this->ismemcache) {
                $this->memcache->delete(MEMCACHE_NAMESPACE . $key);
            } else {
                $files = glob(DIR_CACHE . 'cache.' . $key . '.*');
                if ($files) {
                    foreach ($files as $file) {
                        if (file_exists($file)) {
                            @unlink($file);
                            clearstatcache();
                        }
                    }
                }
            }
        }
    }
    ?>
  3. Check the site is working.