我有一个Repository类,其方法如下:
public function GetOne($id){
$method = __METHOD__;
$post = null;
$post = $this->CacheManager($method, function($id) {
return DB::select("select * from posts where id = ?", [$id]);
});
return $post;
}
我想缓存结果,但是在闭包/回调函数中,$ id参数不起作用。 CacheManager是我在存储库中使用它的一个特征。
public function CacheManager($method, $fn) {
$obj = null;
if(!$this->HasCache($method)){
$obj = $fn();
}else {
$obj = $this->GetCache($method);
}
return $obj;
}
我还有其他一些没有参数的方法,它们正在按预期方式工作。
最佳答案
使用 use
。 :D
使用use
子句,可以将变量从父作用域导入到函数的作用域中。
public function GetOne($id){
$method = __METHOD__;
$post = null;
$post = $this->CacheManager($method, function() use ($id) {
return DB::select("select * from posts where id = ?", [$id]);
});
return $post;
}
只是一个旁注。由于看起来您正在构建缓存机制,因此您还需要在缓存中包括该ID。当前,您仅通过
$method
进行检查,但是对于每个id,您可能会有一个不同的缓存条目,该缓存条目可能存在也可能不存在。因此,我认为在您的函数中,您需要执行以下操作,以使缓存键更具唯一性。我也将参数$method
称为$cacheKey
之类的东西,因为它不应该链接到方法本身的名称到缓存。$method = __METHOD__ . ";$id";
PHP 7.4的更新:箭头函数
RFC for arrow functions(又称“简短关闭”)已通过投票。
使用这些参数时,您无需指定要关闭的参数,因为它们始终只能有一个表达式,因此,它们使用的任何表达式/值都可以(并将)取自父函数范围。
由于在这种情况下,匿名函数只有一个语句,因此可以将其重写为箭头函数。然后,对缓存管理器的调用将如下所示:
public function GetOne($id){
$method = __METHOD__;
$post = null;
$post = $this->CacheManager($method, fn() => DB::select("select * from posts where id = ?", [$id]));
return $post;
}