可使用一品威客账号登录: 登录 立即注册
热搜: 活动 交友 discuz

CI框架实现原理php应用CI框架理解流程PHP好处

查看: 16112 | 回复: 0  Dreamcatcher 发表于 2012-4-19 10:55:44

本帖最后由 Dreamcatcher 于 2012-4-19 10:56 编辑

CI框架缓存的实现原理
今天花了点时间看了下CI框架源码缓存的实现,写出来梳理下思路.
1:在CI框架中加载视图文件使用的是$this->load->view();方法,所以从load类库着手,在ci的system文件夹中可以看到Loader.php,这个类库是在Controller.php中被加载的。Loader类中有个方法:
view source
print?

1function view($view, $vars = array(), $return = FALSE)//加载视图

2{

3return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));

4}



调用了自身的一个私有方法_ci_load(),这个方法其中关键部分在:
view source
print?

01ob_start();//开启缓存

02// If the PHP installation does not support short tags we'll

03// do a little string replacement, changing the short tags

04// to standard PHP echo statements.

05

06if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)

07{

08echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));

09}

10else

11{

12//将视图包含进来

13include($_ci_path); // include() vs include_once() allows for multiple views with the same name

14}

15if (ob_get_level() > $this->_ci_ob_level + 1)

16{

17ob_end_flush();

18}

19else

20{

21$_ci_CI->output->append_output(ob_get_contents());//获取缓存,调用了output类中的append_output方法将缓存的内容放到了output类的全局变量final_output中,供后面使用。

22@ob_end_clean();

23}




2:CI框架中设置缓存的方法是$this->output->cache(n)//n是分钟数
打开system/core/Output.php在里面有个cache方法:
view source
print?

1function cache($time)

2{

3  $this->cache_expiration = ( ! is_numeric($time)) ? 0 : $time;

4//output类中变量cache_expiration赋上缓存时间

5  return $this;

6}





3:打开system/core/Codeigniter.php这个核心文件。可以看到如下代码:
view source
print?

01$OUT =& load_class('Output', 'core');//实例化output类

02

03// 调用钩子 cache_override hook

04if ($EXT->_call_hook('cache_override') === FALSE)//如果没有设置这个缓存钩子就使用默认的_display_cache方法

05{

06  if ($OUT->_display_cache($CFG, $URI) == TRUE)//将config,uri类的对象传入

07  {

08   exit;//如果调用缓存成功就会直接显示页面中断程序,不会加载实例化下面的类,进行一些请求,这就是缓存的好处;

09  }

10}





4:找到Output.php类中的私有方法_display_cache($CFG, $URI):
view source
print?

01function _display_cache(&$CFG, &$URI)

02{

03  //是否在配置文件中定义了缓存路径,如果没有是用系统默认的cache文件夹作为缓存目录

04  $cache_path = ($CFG->item('cache_path') == '') ? APPPATH.'cache/' : $CFG->item('cache_path');

05  // 构造文件路径。文件名是 URI 的 md5 值

06  $uri = $CFG->item('base_url').

07    $CFG->item('index_page').

08    $URI->uri_string;//这是请求的页面的控制器/方法/参数那一串字符

09

10  $filepath = $cache_path.md5($uri);

11

12  // 判断文件是否存在

13  if ( ! @file_exists($filepath))

14  {

15   return FALSE;//到了这里就中断了,而是按照正常的向服务器请求页面内容,下面的return false同理

16  }

17

18  if ( ! $fp = @fopen($filepath, FOPEN_READ))

19  {

20   return FALSE;

21  }

22

23  flock($fp, LOCK_SH);//读取文件前给文件加个共享锁

24

25  $cache = '';

26  if (filesize($filepath) > 0)

27  {

28   $cache = fread($fp, filesize($filepath));

29  }

30

31  flock($fp, LOCK_UN);//释放锁

32  fclose($fp);

33  //  匹配内嵌时间戳

34  if ( ! preg_match("/(\d+TS--->)/", $cache, $match))

35  {

36   return FALSE;

37  }

38

39  // Has the file expired? If so we'll delete it.

40  // 文件过期了,就删掉

41  if (time() >= trim(str_replace('TS--->', '', $match['1'])))

42  {

43   if (is_really_writable($cache_path))

44   {

45    @unlink($filepath);

46    log_message('debug', "Cache file has expired. File deleted");

47    return FALSE  }

48

49  // Display the cache

50  // 显示缓存,到了这里说明有缓存文件并且缓存文件没过期,然后执行_display方法

51  $this->_display(str_replace($match['0'], '', $cache));

52  log_message('debug', "Cache file is current. Sending it to browser.");

53  return TRUE;

54}





5:找到Output方法中的_display($output='')方法,这个 方法有两处调用了,1个是在上述的_display_cache中,将缓存文件中的内容取出赋于$output变量然后传入_display($output='')中,这时候只会执行_display中的:
view source
print?

1//$CI 对象不存在,我们就知道我们是在处理缓存文件,所以简单的输出和退出

2  if ( ! isset($CI))

3  {

4   echo $output;//直接将缓存输出,返回ture中断codeigniter继续执行

5   log_message('debug', "Final output sent to browser");

6   log_message('debug', "Total execution time: ".$elapsed);

7   return TRUE;

8  }




第二处调用是,当if ($OUT->_display_cache($CFG, $URI) == TRUE)这个判断不成立codeigniter向下执行,
先后实例化了一些系统核心类,以及url中请求的控制器方法等.最后执行一个钩子:
view source
print?

1//  调用 display_override hook

2if ($EXT->_call_hook('display_override') === FALSE)

3{

4  $OUT->_display();

5}




这时候执行这个方法是无缓存的情况下. 这时候$output为空所以执行了:
view source
print?

1// 设置输出数据

2  if ($output == '')

3  {

4      $output =& $this->final_output;//这就是在Loader中设置的输出缓存.

5  }




接下来如果执行了$this->output->cache()方法设置了$this->cache_expiration 参数且没有缓存文件时:
view source
print?

1//  启用 cache 时,$CI 没有 _output 函数时,调用 $this->_write_cache,写缓存文件

2  if ($this->cache_expiration > 0 && isset($CI) && ! method_exists($CI, '_output'))

3  {

4   $this->_write_cache($output);

5  }




_write_cache($output)方法如下:
view source
print?

01function _write_cache($output)

02{

03  $CI =& get_instance();

04  $path = $CI->config->item('cache_path');

05  $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

06  //  $cache_path 是目录并且可写

07  if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))

08  {

09   log_message('error', "Unable to write cache file: ".$cache_path);

10   return;

11  }

12

13  $uri = $CI->config->item('base_url').

14    $CI->config->item('index_page').

15    $CI->uri->uri_string();

16

17  $cache_path .= md5($uri);

18

19  if ( ! $fp = @fopen($cache_path, FOPEN_WRITE_CREATE_DESTRUCTIVE))

20  {

21   log_message('error', "Unable to write cache file: ".$cache_path);

22   return;

23  }

24

25  // 加个时间戳,指示过期时间

26  $expire = time() + ($this->cache_expiration * 60);

27

28  if (flock($fp, LOCK_EX))//写入前先加个独占锁

29  {

30   fwrite($fp, $expire.'TS--->'.$output);

31   flock($fp, LOCK_UN);//写完解锁

32  }

33  else

34  {

35   log_message('error', "Unable to secure a file lock for file at: ".$cache_path);

36   return;

37  }

38  fclose($fp);

39  @chmod($cache_path, FILE_WRITE_MODE);

40

41  log_message('debug', "Cache file written: ".$cache_path);

42}




写完缓存后会进行一系列处理比如设置header等 最后输出$output:
view source
print?

1if (method_exists($CI, '_output'))

2  {

3   $CI->_output($output);

4  }

5  else

6  {

7   echo $output;  // Send it to the browser!

8  }





总结:CI的缓存是在要输出的页面设置ob_start(),使用ob_get_contents()获取缓存内容,然后通过判断设置中
是否设置缓存.如果设置了则将缓存将页面的url地址进行MD5哈希作为缓存文件名创建之,然后将(当前时间+设置的缓存时间)+一个特殊符号+内容写到缓存文件中,下次访问时候将访问的url进行MD5查找这个缓存文件,如果没有则再创建.有则取出其中的内容,分离出过期时间和内容,判断时间是否过期,如果过期则丢弃内容,继续进行请求,如果没过期直接取出内容输出到页面,中断执行。CI将这一套缓存机制用面向对象的方法写到了框架中,使用起来很方便。CI默认的这种缓存方法是缓存整个页面。但有时候只要缓存页面中不变的元素header和footer比较好,CI中还有钩子的机制,可以自己设置缓存的方法替换其中的_display_cache()方法。
具体的可以看手册:CI.chm
css3.0参考手册.chm
html5.chm
php中文函数手册.chm
mysql5.1.chm

CI的缓存》url地址进行MD5》取出内容》分离出过期时间和内容,判断时间是否过期,如果过期则丢弃内容,继续进行请求,如果没过期直接取出内容输出到页面。




url地址
网页缓存Codeigniter 支持缓存技术,以达到最快的速度。
尽管CI已经相当高效了,但是网页中的动态内容、主机的内存CPU 和数据库读取速度等因素直接影响了网页的加载速度。依靠网页缓存,你的网页可以达到近乎静态网页的加载速度,因为他们将程序输出的结果保存到硬盘上了。
缓存是怎么工作的?CI支持每个页面单独缓存,而且可以设置缓存更新时间。当一个网页第一次被加载的时候,缓存文件将被保存到application/cache文件夹。下次访问的时候,系统就会直接读取缓存文件,然后返回给用户的浏览器。如果缓存文件过期,它将被删除并重新生成。
注意:Benchmark 标签在使用了缓存的页面仍然可用。
启动缓存启用缓存功能,只需要将下面的代码放入你的任何一个控制器(controller)的方法(function)内:
$this->output->cache(n);
其中 n 是你希望缓存更新的 分钟 数。可以使用 m/60 来精确到秒,例如 1/60 ,则是精确到 1秒
上面的代码可以放到任何一个 function 里面。他的出现顺序对缓存并没有影响,所以将它放在你认为最合乎逻辑的地方。一旦上面的代码放到了控制器的方法中,页面就会被缓存。

警告: 由于CI存储缓存文件的方式,只有通过 view 文件的输出才能被缓存。

注意: 在缓存文件产生之前,请确保 application/cache 文件夹可写。

清除缓存如果你不再想使用缓存,仅需将上面的代码从你的controller里面删除即可。注意: 这样做并不能让缓存文件立即消失,它将会自动过期并被删除。如果你想立即删除那些文件,就必须自己动手了。






使用道具 举报

快速回复
登录后回帖,可以用一品威客账号登录 登录 | 立即注册

一品微博
一品活动
2024线上私享会
2024.3.21,一品威客2024创业领袖线上私享会将火热开播。
新粉福利社
【新粉福利社】新用户最高可领Apple iPad
最新主题热门主题


回顶部
关注
我们
官方微信 官方微博

微信号:epweike
已经有超过10万小伙伴关注了!
更多精彩好玩的活动敬请关注微信公众号

加为关注

微博账号:一品威客网
更多精彩互动尽在一品官方微博
小编期待您的参与