【问题标题】:Opencart only cache if domain not localhost如果域不是本地主机,Opencart 仅缓存
【发布时间】:2012-06-19 08:38:27
【问题描述】:

如果域不是本地主机,我将如何在 opencart 中只写入/获取缓存?

负责缓存的类是;

class Cache { 
    private $expire = 3600; 

    public function __construct() {
        $files = glob(DIR_CACHE . 'cache.*');

        if ($files) {
            foreach ($files as $file) {
                $time = substr(strrchr($file, '.'), 1);

                if ($time < time()) {
                    if (file_exists($file)) {
                        unlink($file);
                    }
                }
            }
        }
    }

    public function get($key) {
        $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

        if ($files) {
            $cache = file_get_contents($files[0]);

            return unserialize($cache);
        }
    }

    public function set($key, $value) {
        $this->delete($key);

        $file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $this->expire);

        $handle = fopen($file, 'w');

        fwrite($handle, serialize($value));

        fclose($handle);
    }

    public function delete($key) {
        $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

        if ($files) {
            foreach ($files as $file) {
                if (file_exists($file)) {
                    unlink($file);
                }
            }
        }
    }
}

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    只是一个快速的补丁......

    在你的 get 函数中

    public function get($key) {
       if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {return;}
    

    在你的集合函数中

    public function set($key, $value) {
       if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {return; }
    

    但请记住,当您即将修补 opencart 的核心时,您可以使用VQmod

    【讨论】:

      【解决方案2】:

      更改get 方法:

          if ($files && $_SERVER['HTTP_HOST'] !== 'localhost') {
      

      您也可以在set 中使用相同的逻辑来节省磁盘写入。

      【讨论】:

        猜你喜欢
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多