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
since method 'keys' exist in
systemlibrarycacheredis.php
to make it GLOBAL you have to add same method insystemlibrarycache.php
then 'keys' method can be call from anywhere like this$this->cache->keys('*')
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
thecache
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
andmem
cache adapters, located onsystem/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 onsystem/config/
.The initialization takes place on the
framework.php
file onsystem/
folder and looks like this:All those classes have their own implementations of the methods that are defined on the
system/library/cache.php
. Namely,get
,set
anddelete
. 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 thekeys
method in any controller, is akeys
method in thesystem/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 corecache
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 methodsget
,set
anddelete
. You can check it here: https://github.com/opencart/opencart/blob/3.0.3.8/upload/system/library/cache/redis.php