php用什么做缓存文件格式,什么是在PHP中缓存文件的最佳方法?

我正在使用我的

PHP代码Smarty,我喜欢缓存一些网站页面,所以我使用以下代码:

// TOP of script

ob_start(); // start the output buffer

$cachefile ="cache/cachefile.html";

// normal PHP script

$smarty->display('somefile.tpl.html') ;

$fp = fopen($cachefile, 'w'); // open the cache file for writing

fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file

fclose($fp); // close the file

ob_end_flush(); // Send the output to the browser

但是当我在php文件的末尾打印ob_get_contents()时它是空的!实际上创建的缓存文件也是空的!所以当我使用smarty时,我怎么能在php中缓存文件我知道我可以使用smarty缓存但是由于某种原因它对我不起作用.

另外请给我关于APC缓存的信息.如何使用它?是否值得在我的情况下使用,我瘦它只是为了缓存数据库查询,我读了关于它的PHP手册,但我什么都得不到:)

坦克.

最佳答案 我已经从文档(位于

here)中剔除了一些代码,以获得更完整的智能缓存示例.另外,我不确定你在你的例子中使用了什么,但是你应该使用smarty的方法来操作缓存.

require('Smarty.class.php');

$smarty = new Smarty;

// 1 Means use the cache time defined in this file,

// 2 means use cache time defined in the cache itself

$smarty->caching = 2;

// set the cache_lifetime for index.tpl to 5 minutes

$smarty->cache_lifetime = 300;

// Check if a cache exists for this file, if one doesn't exist assign variables etc

if(!$smarty->is_cached('index.tpl')) {

$contents = get_database_contents();

$smarty->assign($contents);

}

// Display the output of index.tpl, will be from cache if one exists

$smarty->display('index.tpl');

// set the cache_lifetime for home.tpl to 1 hour

$smarty->cache_lifetime = 3600;

// Check if a cache exists for this file, if one doesn't exist assign variables etc

if(!$smarty->is_cached('home.tpl')) {

$contents = get_database_contents();

$smarty->assign($contents);

}

// Display the output of index.tpl, will be from cache if one exists

$smarty->display('home.tpl');

至于APC缓存,它的工作方式与smarty相同.它们都将数据存储在文件中一段特定的时间.每次要访问数据时,它都会检查缓存是否有效,如果是,则返回缓存值.

但是,如果不使用smarty,您可以使用APC:

这个例子通过将数据库查询的结果存储在缓存中,类似地,您可以修改它来代替存储整个页面输出,这样您就不必经常运行昂贵的PHP函数.

// A class to make APC management easier

class CacheManager

{

public function get($key)

{

return apc_fetch($key);

}

public function store($key, $data, $ttl)

{

return apc_store($key, $data, $ttl);

}

public function delete($key)

{

return apc_delete($key);

}

}

结合一些逻辑,

function getNews()

{

$query_string = 'SELECT * FROM news ORDER BY date_created DESC limit 5';

// see if this is cached first...

if($data = CacheManager::get(md5($query_string)))

{

// It was stored, return the value

$result = $data;

}

else

{

// It wasn't stored, so run the query

$result = mysql_query($query_string, $link);

$resultsArray = array();

while($line = mysql_fetch_object($result))

{

$resultsArray[] = $line;

}

// Save the result inside the cache for 3600 seconds

CacheManager::set(md5($query_string), $resultsArray, 3600);

}

// Continue on with more functions if necessary

}

此示例从here稍加修改.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值