Joomla如何采用缓存加速你的代码 【转】
2010-02-10 13:20
162 查看
在通常网络环境中,同样的内容会一遍遍的显示。缓存通过把程序生成内容的一部分或者全部保存在文件中,需要的时候取出来,这样就使网站提速。有很多方法可以做到这点,但是本文只是针对函数。在Joomla 1.5中,缓存非常容易。
Typically in web environments, the same content is displayed over and over to everyone who visits your site. Caching can speed this up by storing some or all of the information your code generates in a file and returning it when needed. There are different ways to do this, but this tutorial only deals with functions. The good news is that in Joomla! 1.5 it’s dead simple.
1. 利用 Profiler
当使用缓存的时候,首先必须确认使用缓存是有用。 先用Joomla! profiler (JProfiler) 测试一下。
这段将输出:
0.001 seconds to do stuff
再运行一次,你可能看到不同的数字。
2. 要测试的函数
我们首先做一个组件文件/components/com_testcache/testcache.php ,这个文件只是用来测试函数执行的时间。
文件的代码如下:
我们重复执行250次查询,当然现实中不会有这样的代码,但是我们可以看到是否缓存的巨大差异
3. 缓存对象
首先,要确认你在全局配置设置了允许缓存项。
我们可以通过Jfactory取得缓存对象
如果你想在组件中获得缓存,即使全局配置没有启用,你也可以通过以下代码启用缓存:
接下来调用我们的函数
如果你要缓存一个不是在类中的函数,可以不必使用数组,就如以下的代码
可以使用以下代码清除缓存
这将删除缓存目录中的所有缓存的文件和目录。
4. 整合
为了比较,我们将调用两次,一次缓存,一次不缓存,并且我们把两次调用包含在一个profiler中,从而我们可以看清不同。
现在执行 index.php?option=com_testcache. 结果如下:
2.093 without caching
2.160 with caching
第二个稍慢,刷新页面,结果如下:
2.073 without caching
0.008 with caching
在设置的缓存期限内,这个结果大体如此。
5. 参数使用
上述例子中总是重复调用数据,可是通常情况下,函数输入不同的参数而得到不同的结果,这种情况怎么使用缓存?下面的例子展示了怎么解决这个问题
试着调用时候用不同的表
index.php?option=com_testcache&table=content
index.php?option=com_testcache&table=users
index.php?option=com_testcache&table=menu
你都可以看到第一次调用采用缓存稍微慢一些,第二次以后缓存就要快多了。
6. 实际编程
现实中使用缓存并不总能得到更快的结果。 试着去掉例子中循环,你观察一下运行结果。所以说,确认是否缓存有益是必须的。
同样在编程过程中要考虑缓存是非常必要的。
Typically in web environments, the same content is displayed over and over to everyone who visits your site. Caching can speed this up by storing some or all of the information your code generates in a file and returning it when needed. There are different ways to do this, but this tutorial only deals with functions. The good news is that in Joomla! 1.5 it’s dead simple.
1. 利用 Profiler
当使用缓存的时候,首先必须确认使用缓存是有用。 先用Joomla! profiler (JProfiler) 测试一下。
<?php $profiler = new JProfiler(); echo $profiler->mark( ' seconds to do stuff' ); ?>
这段将输出:
0.001 seconds to do stuff
再运行一次,你可能看到不同的数字。
2. 要测试的函数
我们首先做一个组件文件/components/com_testcache/testcache.php ,这个文件只是用来测试函数执行的时间。
文件的代码如下:
<?php // No direct access. defined( '_JEXEC' ) or die( 'Restricted access' ); class TestClass { function testMethod() { $db = & JFactory::getDBO(); for ($i=0; $i<250; $i++) { $db->setQuery( 'SELECT * FROM #__content' ); $rows = $db->loadObjectList(); } return $rows; } } $rows = TestClass::testMethod(); ?>
我们重复执行250次查询,当然现实中不会有这样的代码,但是我们可以看到是否缓存的巨大差异
3. 缓存对象
首先,要确认你在全局配置设置了允许缓存项。
我们可以通过Jfactory取得缓存对象
<?php $cache = & JFactory::getCache(); ?>
如果你想在组件中获得缓存,即使全局配置没有启用,你也可以通过以下代码启用缓存:
<?php $cache->setCaching( 1 ); ?>
接下来调用我们的函数
<?php $rows = $cache->call( array( 'TestClass', 'testMethod' ) ); ?>
如果你要缓存一个不是在类中的函数,可以不必使用数组,就如以下的代码
<?php $rows = $cache->call( 'testFunction' ); ?>
可以使用以下代码清除缓存
<?php $cache->cleanCache(); ?>
这将删除缓存目录中的所有缓存的文件和目录。
4. 整合
为了比较,我们将调用两次,一次缓存,一次不缓存,并且我们把两次调用包含在一个profiler中,从而我们可以看清不同。
<?php class TestClass { function testMethod() { $db = & JFactory::getDBO(); for( $i=0; $i<250; $i++) { $db->setQuery( 'SELECT * FROM #__content' ); $rows = $db->loadObjectList(); } return $rows; } } $cache = & JFactory::getCache(); $profiler = new JProfiler(); $rows = TestClass::testMethod(); echo $profiler->mark( ' without caching' ); $profiler = new JProfiler(); $rows = $cache->call( array( 'TestClass', 'testMethod' ) ); echo $profiler->mark( ' with caching' ); ?>
现在执行 index.php?option=com_testcache. 结果如下:
2.093 without caching
2.160 with caching
第二个稍慢,刷新页面,结果如下:
2.073 without caching
0.008 with caching
在设置的缓存期限内,这个结果大体如此。
5. 参数使用
上述例子中总是重复调用数据,可是通常情况下,函数输入不同的参数而得到不同的结果,这种情况怎么使用缓存?下面的例子展示了怎么解决这个问题
<?php class TestClass { function testMethod( $table ) { // Get a reference to the global database object. $db = & JFactory::getDBO(); // Execute the same database query 250 times. for( $i=0; $i<250; $i++) { $db->setQuery( "SELECT * FROM #__$table" ); $rows = $db->loadObjectList(); } return $rows; } } // Determine which database table to query. $table = JRequest::getVar( 'table', 'content' ); // Get a reference to the global cache object. $cache = & JFactory::getCache(); // Run the test without caching. $profiler = new JProfiler(); $rows = TestClass::testMethod( $table ); echo $profiler->mark( ' without caching' ); // Run the test with caching. $profiler = new JProfiler(); $rows = $cache->call( array( 'TestClass', 'testMethod' ), $table ); echo $profiler->mark( ' with caching' ); ?>
试着调用时候用不同的表
index.php?option=com_testcache&table=content
index.php?option=com_testcache&table=users
index.php?option=com_testcache&table=menu
你都可以看到第一次调用采用缓存稍微慢一些,第二次以后缓存就要快多了。
6. 实际编程
现实中使用缓存并不总能得到更快的结果。 试着去掉例子中循环,你观察一下运行结果。所以说,确认是否缓存有益是必须的。
同样在编程过程中要考虑缓存是非常必要的。
相关文章推荐
- 如何让你的网站提速:Web缓存加速指南(二)
- 敏捷开发代码管理规则:为什么要采用单一主干代码管理?如何做到单一主干代码管理?
- 如何用php代码清空squid缓存下的指定文件
- Android的一段常用动画效果代码(如何让点击的图片控件加速飞入到指定位置)
- 关于PHP如何用代码删除Runtime下的文件,做到物理删除缓存
- 详解如何利用Cython为Python代码加速
- php如何采用memcache技术 加速web应用运行效率
- 如何在Joomla中添加Google的网站分析代码
- 采用CDN加速后,如何在程序里获取用户IP地址
- Android的一段常用动画效果代码(如何让点击的图片控件加速飞入到指定位置)
- 发挥缓存的威力,提高代码效率,及如何实现16位浮点数
- 如何通过ODBC驱动代码连接Sql Server数据库--采用C#语言
- 如何清理asp站点缓存的代码
- 发挥缓存的威力,提高代码效率,及如何实现16位浮点数
- 在Joomla中如何用代码形式应用Modules 【转】
- day02_ndk常见错误&如何采用log调试代码
- 如何写出运行速度更快的代码:软件篇——加速k均值的代码(OpenMP)
- 如何在Windows中编译Linux Unix的代码(采用cygwin)?
- js和HTML结合(补充知识:如何防止文件缓存的js代码)
- 如何让你的网站提速:Web缓存加速指南(一)