讨论数量:
Cache::put()
实际上调用的是 \Illuminate\Cache\Repository.php
中的 put()
方法:
/**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return void
*/
public function put($key, $value, $minutes = null)
{
if (is_array($key)) {
$this->putMany($key, $value);
return;
}
if (! is_null($minutes = $this->getMinutes($minutes))) {
$this->store->put($this->itemKey($key), $value, $minutes);
$this->event(new KeyWritten($key, $value, $minutes));
}
}
看代码,并没有规定第二个参数必须是数组,实际上都可以。我把教程中的代码修改成这样,使用数组保存用户实体,结果也是正常的:
$active_users = array(); // 使用数组
foreach ($users as $user_id => $user) {
// 尝试查找用户
$user = $this->find($user_id);
// 如果数据库里有该用户的话
if ($user) {
// 将此用户实体放入数组的末尾
array_push($active_users, $user);
}
}
推荐文章: