如何在PHP中正确地进行函数缓存中的类

如何在PHP中正确地进行函数缓存中的类,php,class,caching,static,Php,Class,Caching,Static,在PHP中实现函数缓存类的最佳实践方法是什么。现在我正在做: Class Functions { public static function foo() { static $local_result; if(!empty($local_result) { return $local_result; } $result = some_slow_http_request();

在PHP中实现函数缓存类的最佳实践方法是什么。现在我正在做:

Class Functions {
     public static function foo() {
         static $local_result;

         if(!empty($local_result) {
             return $local_result;
         }      

         $result = some_slow_http_request();
         $local_result = $result;
         return $result;
     }
}
因此:


有更好的办法吗?如果其他地方的代码更改了$local\u result,认为它是一个单独的变量,那么名称空间冲突是否可能?

$local\u result是函数foo的一个局部变量,唯一的区别是它在函数调用后不会被删除。这是一种很好的基于每个请求进行缓存的方法,不过您可能需要查看Memcache、Opcache和APC。请记住,这种方法仅对服务器的每个请求进行缓存。我的意思是,如果你运行这个脚本两次,缓存就不会保留在第二个脚本中。
// First request makes an http request
Functions::foo();

// Second time, pull from memory, does not make an http request
Functions::foo();