英文:
How to add new method in Opencart 2 - Redis
问题
When you attempt to call the $this->cache->keys('*')
method in any controller, you receive the following error: "Uncaught Error: Call to undefined method Cache::keys() in /catalog/controller/product/category.php."
However, when you call $this->cache->set($key, $value)
or $this->cache->get($key)
methods in any controller, they function correctly.
To enable the keys
method to work in the same way as the set
and get
methods, you can modify the system\library\cache\redis.php
file as follows:
<?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(CACHE_PREFIX . $key); // Modify this line
}
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));
}
}
By adding CACHE_PREFIX .
before $key
in the keys
method, you ensure that the keys
method uses the same cache prefix as the set
and get
methods, resolving the issue.
英文:
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));
}
}
答案1
得分: 1
"自从方法 'keys' 存在于 system\library\cache\redis.php
中,要使其成为全局的,你需要在 system\library\cache.php
中添加相同的方法,然后可以在任何地方像这样调用 'keys' 方法 $this->cache->keys('*')
。"
英文:
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('*')
答案2
得分: -1
以下是翻译好的内容:
你可以这样做,但最好不要这样做。我会解释我的理由,但首先需要澄清一下cache
对象在OC上的工作方式:
当你调用$this->cache->SOME_METHOD_NAME
时,cache
对象实际上位于这里:system/library/cache.php
。
这个对象在初始化时以缓存驱动程序(或适配器或类型)类作为参数,实际上是缓存引擎类型的不同实现。
例如,在OC2上,默认情况下有apc
、file
和mem
缓存适配器,位于system/library/cache/
文件夹中。
你可以通过更改以下条目来更改存储使用的缓存类型:
$_['cache_type'] = 'file'; // apc, file, or mem
在default.php
配置文件中,该文件位于system/config/
中。
初始化过程发生在system/
文件夹中的framework.php
文件中,如下所示:
// 缓存
$registry->set('cache', new Cache($config->get('cache_type'), $config->get('cache_expire')));
所有这些类都有自己在system/library/cache.php
中定义的方法的实现。具体来说,get
、set
和delete
。这样,我们可以更改缓存适配器,而无需更改我们或第三方扩展调用缓存对象的任何代码!
现在我们已经看到了cache
对象的工作原理,为了在任何控制器中使用keys
方法,你需要在system/library/cache.php
类中添加一个keys
方法。
然而,其他缓存适配器也没有keys
方法,这意味着,如果你在核心cache
类中进行更改,你将无法更改缓存类型,而不首先向它们添加一个keys
方法,这可能对它们没有任何意义。
最后,你应该考虑是否真的需要在你的Redis适配器中使用keys
方法。
例如,在最新的OC v3.x.x.x中,默认情况下有一个redis
适配器,仅使用常见的缓存方法get
、set
和delete
。你可以在这里查看它:https://github.com/opencart/opencart/blob/3.0.3.8/upload/system/library/cache/redis.php
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论