【问题标题】:Why can't serialize 'closure' for the first time only?为什么不能只序列化“闭包”第一次?
【发布时间】:2020-03-18 11:20:21
【问题描述】:

错误是:

不允许序列化“闭包”

错误:

.../vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php:295

第一次在 Cache 上永远记住时抛出。 在第二次尝试后(重新加载浏览器时)它可以正常工作。

public function cache() 
{
    $task = $this;

    return Cache::rememberForever('apply:' . $task->apply->slug . ':' . $task->slug, function () use ($task) {
        return $task;
    });
}

有趣的部分是这个。所以它适用于在 Apply 的索引页面上缓存 $apply。 (代码相同)

注意:此问题与 Redis 直接相关。请不要提及关于序列化的老问题。你也可以查看官方 Laravel 6.x 文档。与它相关的所有内容都添加了:https://laravel.com/docs/6.x/cache#retrieving-items-from-the-cache

【问题讨论】:

标签: laravel caching redis closures


【解决方案1】:

如果缓存中存在数据,我通过手动存储和返回数据来修复它(rememberForever() 应该如何工作)。

public function cache () {

    $slug = 'task:'.$this->slug;

    if(Cache::has($slug)) return Cache::get($slug);

    if(!Cache::put($slug, $this)) throw new ProtocolException(1045);

    return Cache::get($slug);
}

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2015-05-11
    • 2017-05-29
    • 2019-11-18
    • 2015-12-17
    • 2019-10-08
    相关资源
    最近更新 更多