Laravel 5.4 - 缓存数组

Sas*_*sha 4 laravel laravel-5 laravel-5.4

我正在尝试缓存一个数组,但由于某种原因,没有添加任何内容。这是代码:

public static function getEmployees()
    {

        if(!Cache::has('employees')):

            $data      = ['urlPath' => '/employees/directory'];

            $params    = ['fields' => 'jobTitle'];

            $employees = API::callAPI('GET', $data, $params);

            Cache::putMany($employees, 1440);

        endif;

        return Cache::get('employees');

    }
Run Code Online (Sandbox Code Playgroud)

并且当我尝试获取缓存值(数组)时,我得到了空值:

dd(Cache::get('employees'));
Run Code Online (Sandbox Code Playgroud)

这是我要存储的数组:

array:2 [?
  "fields" => array:16 [?]
  "employees" => array:257 [?]
]
Run Code Online (Sandbox Code Playgroud)

(我正在使用 db 进行存储)

Mar*_*ski 6

你用putMany()错了。我敢打赌,您只需要使用常规就足够了put()

Cache::put('employees', $employees, 1440);
Run Code Online (Sandbox Code Playgroud)

但是如果你愿意,putMany()那么你需要先准备源数组,你没有这样做:

$data = [
    'fields' => whateverItComesFrom(),
    'employees' => API::callAPI('GET', $data, $params),
];
Cache::putMany($data, 1440);
Run Code Online (Sandbox Code Playgroud)

编辑

正如评论中提到的其他用户,除了不正确的使用外,数据库存储也可能导致问题,因为根据您要缓存的数据的大小,它可能只是超出数据库类型限制,即 BLOB仅为 65535 字节 (64KB) (这对于大多数情况来说就足够了,但是数组中有 200 多个条目......)。MEDIUMBLOB是 16777215 字节(16 MB)和LONGBLOB4294967295 字节(4 GB),因此可能值得检查该方面并在需要时更改列类型。