Laravel缓存:数据无效

4

我有两个函数看起来非常相似,它们被一个JavaScript函数作为AJAX请求依次调用。

/**
     * get all Airports for autocomplete
     */
    public function getAirports(){
        if(Cache::has('airports')){
            return Cache::get('airports');
        }

        $airportModel = new Airport;

        $airports = json_encode($airportModel -> _getForAutocomplete('iata_faa_code'));

        Cache::put('airports', $airports, 600);

        return $airports;
    }

    /**
     * get all Countries for autocomplete
     */
    public function getCountries(){
        if(Cache::has('countries')){
            return Cache::get('countries');
        }

        $countryModel = new Country;

        $countries = json_encode($countryModel -> _getForAutocomplete('two_letter_code'));

        Cache::put('countries', $countries, 600);

        return $countries;
    }

现在,当我第一次访问页面时,我能够正确地获取数据(因为它还没有被缓存)。如果我第二次访问页面,我能够获得国家的信息,但是对于机场的信息,我会收到以下错误,并且无法看出原因。
{"error":{"type":"Illuminate\\Encryption\\DecryptException","message":"Invalid data.","file":"C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\Encrypter.php","line":132}}
通过谷歌搜索和删除缓存部分,我已经发现这与缓存有关。如果有人能够帮助我解决这个问题,我将不胜感激。
顺便说一下,我正在使用数据库作为我的缓存驱动程序。
最好的问候,Marcel
1个回答

7

我认为序列化存在一些可疑的问题。在序列化过程中,$airports可能会被破坏。

如果airports字符串的值过长而无法存储到MySQL字段中,则该字段将会被默默地截断,导致数据解密失败。

Laravel默认建议将value字段类型设置为text。如果需要更大的空间,可以使用mediumText或longText字段。


我无法进入if块。如果在if块之前执行dd(Cache::get('airports'))dd(Cache::has('airports')),我会得到相同的错误。 - molerat
您可以使用 $value = Crypt::decrypt($encrypted); 来解密一个加密的字符串。 - Margus Pala
当我进行解密时,我遇到了相同的错误。所以也许是解密部分引起了这个错误? - molerat
也许我想要存储的数据太大了?我有8300个机场,而只有225个国家。 - molerat
3
使用mediumText或longText代替text作为字段的值。 - Margus Pala
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接