我有一个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更新:箭头函数
使用这些参数时,您无需指定要关闭的参数,因为它们始终只能有一个表达式,因此,它们使用的任何表达式/值都可以(并将)取自父函数范围。
由于在这种情况下,匿名函数只有一个语句,因此可以将其重写为箭头函数。然后,对缓存管理器的调用将如下所示:
public function GetOne($id){
$method = __METHOD__;
$post = null;
$post = $this->CacheManager($method, fn() => DB::select("select * from posts where id = ?", [$id]));
return $post;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句