Php 单态ish模式在CodeIgniter中未按预期工作
我刚刚启动了一个小型库,它需要从各种URL中筛选scrape并搜索指定的字符串。为了提高性能,我希望缓存检索到的页面的内容(在请求期间,所以在内存中) 我现在有这个:Php 单态ish模式在CodeIgniter中未按预期工作,php,codeigniter,caching,Php,Codeigniter,Caching,我刚刚启动了一个小型库,它需要从各种URL中筛选scrape并搜索指定的字符串。为了提高性能,我希望缓存检索到的页面的内容(在请求期间,所以在内存中) 我现在有这个: class Scraper { private $CI; private $Cache; function __construct() { $this->CI =& get_instance(); $Cache = array(); }
class Scraper {
private $CI;
private $Cache;
function __construct() {
$this->CI =& get_instance();
$Cache = array();
}
public function GetPage($Url) {
if(!isset($Cache[$Url])) {
dump("Retrieving");
$Cache[$Url] = "DATA";//file_get_contents($Url);
}
return $Cache[$Url];
}
public function FindString($Url, $String) {
$Contents = $this->GetPage($Url);
$Ret = (strpos(strtolower($Contents), strtolower($String)) !== false);
return $Ret;
}
}
注意:为了在调试时提高性能,我只是将“数据”转储到缓存中,而不是获取页面
现在,我有一个循环,它使用相同的URL反复调用FindString()
我希望第一次调用打印出“retrieving”,然后再看不到其他内容。事实上,我反复看到“检索”
我怀疑我在某个地方遇到了范围问题-要么库本身不是一个单例,因此对FindString
的每次调用都会到达一个唯一的实例-要么Cache
变量正在以某种方式重新初始化
有人可以建议调试的下一步吗
(dump()
只是为我很好地格式化了东西)在访问实例变量$Cache
的所有地方都缺少一个$this
。代码应为:
class Scraper {
private $CI;
private $Cache;
function __construct() {
$this->CI =& get_instance();
$this->Cache = array();
}
public function GetPage($Url) {
if(!isset($this->Cache[$Url])) {
dump("Retrieving");
$this->ache[$Url] = "DATA";//file_get_contents($Url);
}
return $this->Cache[$Url];
}
public function FindString($Url, $String) {
$Contents = $this->GetPage($Url);
$Ret = (strpos(strtolower($Contents), strtolower($String)) !== false);
return $Ret;
}
}
看不见森林的树木。很明显。非常感谢。