【发布时间】:2020-01-18 21:55:32
【问题描述】:
这是我第一次使用 Laravel 和 Redis。我了解如何在终端上获取、设置 Redis 等。但不知道如何在 Laravel 应用程序上应用 Redis。 我有使用 MVC 模式将参与者的信息保存在数据库中的应用程序。我想将其更改为使用 Redis 缓存以使其更快(并用于练习)。我需要做什么? 你能用代码解释一下吗?
这是参与者控制器。 “编辑”功能将用户发送到编辑页面,用户编辑信息并按下“保存”,激活“更新”功能。 store/updateUserInput 函数只是将数据保存到数据库中。
/**
* Show the form for editing the specified participant.
*
* @param int $id
* @return View
*/
public function edit(int $id): View
{
$participant = Participant::find($id);
if(empty($participant)){
return view('errors.404');
}
return view('participants.edit', ['participant'=>$participant]);
}
/**
* Update the specified participant in storage.
*
* @param ParticipantValidation $request
* @return RedirectResponse
*/
public function update(ParticipantValidation $request): RedirectResponse
{
$participant = Participant::find($request->id);
if(empty($participant)){
return view('errors.404');
}
$detail = $request->all();
Participant::updateUserInput($detail);
return redirect()->route('participants.create', $detail['event_id'])->with('success', 'Updated!');
}
+加上我在“控制器”上尝试使用此代码以使用 $redis->set('message', 'Hello world'); 之类的东西,但出现错误,他们找不到“Predis/Autoload.php”
require 'Predis/Autoload.php';
PredisAutoloader::register();
try {
$redis = new PredisClient();
}
catch (Exception $e) {
die($e->getMessage());
}
【问题讨论】:
标签: php laravel caching redis predis