Laravel cache :: remember-无法序列化闭包

问题描述 投票:0回答:1

尝试使用缓存记住时,我得到以下异常:

enter image description here

但是在Laravel文档(https://laravel.com/docs/5.7/cache)中,他们在示例中使用了闭包:

closure being used in Lara docs example

任何帮助,非常感激……用谷歌搜索,人们似乎对无法永久化forever()闭包有问题(但建议的解决方案不适用于我)

/**
     * @param string $guid
     * @return Account
     */
    public function getAccount(string $guid): Account
    {
        $key = md5(sprintf('xero/accounts[guid="%s"]', $guid));
        return $this->cache->remember($key, Carbon::now()->addHour(), function () use ($guid) {
            return $this->xero->loadByGUID(Account::class, $guid);
        });
    }

我现在也尝试这样做(以避开将闭包传递给cache ::记住fxn):

public function getAccount(string $guid): Account
    {
        $key = md5(sprintf('xero/accounts[guid="%s"]', $guid));

        $account = $this->cache->get($key);
        if ($account === null) {
            //dump('account not found, storing in cache...');
            /** @var Account $account */
            $account = $this->xero->loadByGUID(Account::class, $guid);
            $this->cache->put($key, $account, Carbon::now()->addHour());
        }
    }

但在'$ this-> cache-> put($ key,$ account,Carbon :: now()-> addHour())行仍然出现相同的错误(无法序列化Closure);'

$ account对象的类型:使用XeroPHP \ Models \ Accounting \ Account;(来自https://github.com/calcinai/xero-php

laravel caching laravel-5.7
1个回答
0
投票

模型包含对Xero应用程序的引用,该引用包含Guzzle客户端的实例,该实例本身具有包含闭包的属性。PHP的序列化功能无法序列化闭包:https://3v4l.org/1MIpd

存储和检索模型时,可能会调用toStringArray和fromStringArray。

(对Josh-G的功劳,https://github.com/calcinai/xero-php/issues/734

© www.soinside.com 2019 - 2024. All rights reserved.