skip to Main Content

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?

systemlibrarycacheredis.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));
        }
    }

2

Answers


  1. Chosen as BEST ANSWER

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


  2. You can, but better not to do so. I’ll explain my reasoning but first some clarifications on how cache object works on OC:

    When you are calling $this->cache->SOME_METHOD_NAME the cache object is actually the one located here: system/library/cache.php.

    This one, when initialized takes as argument a cache driver (or adapter or type) class, which is actually the different implementations of the cache engine types.
    e.g. on OC2 by default there are the apc, file and mem cache adapters, located on system/library/cache/ folder.

    And you can change the cache type your store uses by changing the following entry:

    $_['cache_type'] = 'file'; // apc, file or mem

    in the default.php config file, found on system/config/.

    The initialization takes place on the framework.php file on system/ folder and looks like this:

    // Cache 
    $registry->set('cache', new Cache($config->get('cache_type'), $config->get('cache_expire')));
    

    All those classes have their own implementations of the methods that are defined on the system/library/cache.php. Namely, get, set and delete. This way we can change the cache adapter and we will not need to change anything on our code where we or 3rd party extensions are calling the cache object!

    Now that we have seen how the cache object works, what you are missing in order to use the keys method in any controller, is a keys method in the system/library/cache.php class.

    However, the other cache adapters do not have the keys method as well, meaning, that if you make the change in the core cache class, you will not be able to change cache types without first add to them also a keys method, which probably will not have any meaning on them.

    In the end you should think if you really need the keys method in your redis adapter.

    For instance, in the latest OC v3.x.x.x there is a redis adapter by default and only using the common cache methods get, set and delete. You can check it here: https://github.com/opencart/opencart/blob/3.0.3.8/upload/system/library/cache/redis.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search