Heim  >  Artikel  >  Backend-Entwicklung  >  PHP缓存详谈_PHP教程

PHP缓存详谈_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:25:21761Durchsuche

大家会问什么是缓存!能干什么!其实缓存相当于内存。保存一段时间!

缓存就是我们执行东西时候不用在执行数据库了。直接执行我们的缓存就OK了

一般来说,缓存的目的是把数据放在一个地方让访问的更快点,毫无疑问,内存是最快的,但是,几百M的数据能往内存放么?这不现实,当然,有的时候临时放如服务器缓存,如ob_start()这个缓存页面开启的话在发送文件头之前页面内容都被缓存在内存中,知道等页面输出自动清楚或者等待ob_get_contents的返回,或者被ob_end_clean显示的清除,这在静态页面的生成中能很好的利用,在模板中能得到很好的体现,我的这篇文章深入的讨论了:谈PHP生成静态页面,这是一种方式,但这是临时性的,不是解决我们问题的好方法.

可以这么说:缓存一般分为页面缓存和数据缓存。ADODB缓存是数据缓存.smarty是页面缓存。  adodb缓存是

<?php }include(./adodb/adodb.inc.php); $ADODB_CACHE_DIR='tmp'; $db=NewADOConnect('mysql'); $db->connect('localhost','root','123456','mysql'); $sql="select * from user";  $db->cacheexecute(300,$sql); ?>

这样在TMP目录下生成了缓存!(缓存文件是序列化的数据。) 当下次在执行的时候,我们直接从缓存里面读取数据。SMARTY缓存:

<?phprequire('./smarty/Smarty.class.php'); $smarty = new Smarty; Z)$smarty->caching = true;if(!$smarty->is_cached('index.tpl'))      // No cache available, do variable assignments here. )      $contents = get_database_contents();     $smarty->assign($contents);} $smarty->display('index.tpl'); )?>

这个首先判断是否有这个缓存文件!没有直接链接数据库!有的话!执行DISPLAY。就是读取缓存。大家看到上面的2个例子!对缓存有很大的理解了吧!


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446694.htmlTechArticle大家会问什么是缓存!能干什么!其实缓存相当于内存。保存一段时间! 缓存就是我们执行东西时候不用在执行数据库了。直接执行我们的...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn