2.14.3.9. Налаштування Memcache(d) в OpenCart

Процес налаштування Memcache(d) відрізняється в різних версіях OpenCart.

  1. В кінці кожного з двох конфігураційних файлів додайте такий код:
    // CACHE
    define('CACHE_HOSTNAME', '/home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    У рядку з параметром CACHE_HOSTNAME замість example вкажіть назву хостинг-акаунту, в якому розміщений сайт.

  2. Відредагуйте файл default.php, розташований в каталозі system/config:
    • Знайдіть такий блок коду:
      // Cache
      $_['cache_engine'] = 'file'; // apc, file, mem or memcached
      $_['cache_expire'] = 3600;
    • У рядку з параметром cache_engine замініть file на memcached, щоб блок коду прийняв такий вигляд:
      // Cache
      $_['cache_engine'] = 'memcached'; // apc, file, mem or memcached
      $_['cache_expire'] = 3600;
  3. Перевірте роботу сайту.
  1. В кінці кожного з двох конфігураційних файлів додайте такий код:
    // CACHE
    define('CACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    У рядку з параметром CACHE_HOSTNAME замість example вкажіть назву хостинг-акаунту, в якому розміщений сайт.

  2. Відредагуйте файл default.php, розташований в каталозі system/config:
    • Знайдіть такий блок коду:
      // Cache
      $_['cache_type'] = 'file'; // apc, file or mem
      $_['cache_expire'] = 3600;
    • У рядку з параметром cache_type замініть file на mem, щоб блок коду прийняв такий вигляд:
      // Cache
      $_['cache_type'] = 'mem'; // apc, file or mem
      $_['cache_expire'] = 3600;
  3. Перевірте роботу сайту.
  1. В кінці кожного з двох конфігураційних файлів додайте такий код:
    // CACHE
    define('CACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('CACHE_PORT', '0');
    define('CACHE_PREFIX', 'oc_');

    У рядку з параметром CACHE_HOSTNAME замість example вкажіть назву хостинг-акаунту, в якому розміщений сайт.

  2. Відредагуйте два файли index.php, один з яких знаходиться в кореневому каталозі сайту, другий — в підкаталозі admin:
    • Знайдіть такий блок коду:
      // Cache
      $cache = new Cache('file');
      $registry->set('cache', $cache);
    • У рядку зі змінною $cache замініть file на mem, щоб блок коду прийняв такий вигляд:
      // Cache
      $cache = new Cache('mem');
      $registry->set('cache', $cache);
  3. Перевірте роботу сайту.
  1. В кінці кожного з двох конфігураційних файлів додайте такий код:
    define('CACHE_DRIVER', 'memcached');
    define('MEMCACHE_HOSTNAME', 'unix:///home/example/.system/memcache/socket');
    define('MEMCACHE_PORT', '0');
    define('MEMCACHE_NAMESPACE', 'opencart_test');

    У рядку з параметром MEMCACHE_HOSTNAME замість example вкажіть назву хостинг-акаунту, в якому розміщений сайт.

  2. Замініть весь вміст файлу system/library/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. Перевірте роботу сайту.
Зміст