Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件缓存问题-隔一段时间会出现一次 #2280

Closed
pgyf opened this issue Jun 4, 2020 · 14 comments
Closed

文件缓存问题-隔一段时间会出现一次 #2280

pgyf opened this issue Jun 4, 2020 · 14 comments

Comments

@pgyf
Copy link

pgyf commented Jun 4, 2020

隔一段时间会出现一次这个异常

Argument 1 passed to think\cache\Driver::unserialize() must be of the type string, boolean given

@hongweipeng
Copy link
Member

哪个TP版本?

@hongweipeng
Copy link
Member

我也遇到了这个问题,我用的是文件缓存。

@pgyf
Copy link
Author

pgyf commented Jun 11, 2020

@hongweipeng 6.0.1 就是用的文件缓存

@pgyf
Copy link
Author

pgyf commented Jun 11, 2020

昨天又出现了

@hongweipeng
Copy link
Member

我改了框架的代码,暂时解决了。

@hongweipeng
Copy link
Member

等官方解决方案或者我一会提交个PR

@pgyf
Copy link
Author

pgyf commented Jun 12, 2020

@hongweipeng 怎么改的

@hongfs
Copy link
Contributor

hongfs commented Jun 13, 2020

vendor\topthink\framework\src\think\cache\driver\File.php

public function get($name, $default = null)
{
    $this->readTimes++;

    $raw = $this->getRaw($name);

    if(is_null($raw)) {
        return $default;
    }

    if(is_bool($raw)) {
        return $raw === false ? $default : $raw;
    }

    return $this->unserialize($raw['content']);
}

上面方法应该可以处理这个问题。问题的引起应该是 $this->getRaw 方法里面的 file_get_contents 读取数据失败然后返回 false 照成。

@hongweipeng
Copy link
Member

@phpyii 这么改的 https://github.com/hongweipeng/framework/pull/2/files

@hongweipeng
Copy link
Member

据反馈,有人用 redis驱动也出现了同样的问题,所以不一定是文件缓存驱动的问题。关联issue:top-think/think-cache#6

@pgyf
Copy link
Author

pgyf commented Jun 24, 2020

@hongweipeng
但是自从我改了后 文件缓存没有出现过这个问题了
修改如下:
修改位置 https://github.com/top-think/framework/blob/6.0/src/think/cache/driver/File.php#L139

    /**
     * 读取缓存
     * @access public
     * @param string $name    缓存变量名
     * @param mixed  $default 默认值
     * @return mixed
     */
    public function get($name, $default = null)
    {
        $this->readTimes++;

        $raw = $this->getRaw($name);
        if(is_null($raw)){
            return $default;
        }
        if(!empty($raw['content']) ){
            try {
                return $this->unserialize($raw['content']);
            } catch (\Exception $exc) {
            }
        }
        return $default;
    }

@hongweipeng
Copy link
Member

@phpyii 殊途同归。但也只能算是缓解而已,这个方案会把缓存意外视为不存在,实际上它是存在的= =

@hongweipeng
Copy link
Member

欢迎给出更好的方案哈。

@gaoyia
Copy link
Contributor

gaoyia commented Aug 25, 2020

#2349

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants