在Opencart 2中添加新方法 – Redis

huangapple go评论63阅读模式
英文:

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-&gt;cache-&gt;keys(&#39;*&#39;) 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-&gt;cache-&gt;set($key, $value) or $this-&gt;cache-&gt;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

  &lt;?php
    namespace Cache;

    class Redis {
        private $expire;
        private $cache;

        public function __construct($expire) {
            $this-&gt;expire = $expire;
            $this-&gt;cache = new \Redis();
            $this-&gt;cache-&gt;pconnect(CACHE_HOSTNAME, CACHE_PORT);
        }

        public function keys($key) {
          return $this-&gt;cache-&gt;keys($key);
        }

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

        public function set($key, $value) {
          return $this-&gt;cache-&gt;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-&gt;cache-&gt;keys(&#39;*&#39;)

答案2

得分: -1

以下是翻译好的内容:

你可以这样做,但最好不要这样做。我会解释我的理由,但首先需要澄清一下cache对象在OC上的工作方式:

当你调用$this->cache->SOME_METHOD_NAME时,cache对象实际上位于这里:system/library/cache.php

这个对象在初始化时以缓存驱动程序(或适配器或类型)类作为参数,实际上是缓存引擎类型的不同实现。
例如,在OC2上,默认情况下有apcfilemem缓存适配器,位于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中定义的方法的实现。具体来说,getsetdelete。这样,我们可以更改缓存适配器,而无需更改我们或第三方扩展调用缓存对象的任何代码!

现在我们已经看到了cache对象的工作原理,为了在任何控制器中使用keys方法,你需要在system/library/cache.php类中添加一个keys方法。

然而,其他缓存适配器也没有keys方法,这意味着,如果你在核心cache类中进行更改,你将无法更改缓存类型,而不首先向它们添加一个keys方法,这可能对它们没有任何意义。

最后,你应该考虑是否真的需要在你的Redis适配器中使用keys方法。

例如,在最新的OC v3.x.x.x中,默认情况下有一个redis适配器,仅使用常见的缓存方法getsetdelete。你可以在这里查看它: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-&gt;cache-&gt;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:

$_[&#39;cache_type&#39;] = &#39;file&#39;; // 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-&gt;set(&#39;cache&#39;, new Cache($config-&gt;get(&#39;cache_type&#39;), $config-&gt;get(&#39;cache_expire&#39;)));

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

huangapple
  • 本文由 发表于 2023年5月22日 20:40:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定