phpopencartopencart2.x

How to add new method in Opencart 2 - Redis


Why when i try to call $this->cache->keys('*') method in any controller im getting error: Uncaught Error: Call to undefined method Cache::keys() in /catalog/controller/product/category.php

When i call $this->cache->set($key, $value) or $this->cache->get($key) methods in any controller they work fine.

How to make method keys to work as set and get methods?

system\library\cache\redis.php

  <?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 keys($key) {
          return $this->cache->keys($key);
        }

        public function get($key) {
          return $this->cache->get(CACHE_PREFIX . $key);
        }

        public function set($key, $value) {
          return $this->cache->set(CACHE_PREFIX . $key, json_encode($value));
        }
    }

Solution

  • since method 'keys' exist in system\library\cache\redis.php to make it GLOBAL you have to add same method in system\library\cache.php then 'keys' method can be call from anywhere like this $this->cache->keys('*')