Tag: Tricks

Now days the WebPages are outcome of heavy script processing in order to provide more dynamicity to page visitors. The utility of server side scripting is to adopt more engineering on web. When the page gets more visitors or becomes more popular, you have to ensure that, that traffic doesn’t stack or queued in front of your freaky web server. All you have to do is to serve them with your page and ofcourse serve it faster. Again server side scripting requires processing and compiling.

In order to serve pages faster, to make light use of server side processing and compiling and to survive within heavy traffic, caching dynamic script output is needed.

The Need For Speed…

Every time a request hits your web server, PHP has to do a lot of processing, all of your codes have to be compiled and executed for a single traffic hit every time. Interesting thing will be if the outcomes of all these processing is identical for each visitors. Say, processing happens every time for visitor 24500 and 24501 while the outputs are so same. Argh!!

What would be if we save the flat HTML generated for visitor 24500 and serve that to 24501 as well??? That will be awesome 😀 coz this leads to less processing and faster page handover. This da mechanism we are talking about, yeah! Cache PHP output 😀

Well we can write such optimization system but there is a smart package in PEAR called Cache_Lite that can do this job for us. Let’s check out why Cache_Lite:

It saves time of writing new caching codes 😀

It’s optimized for high traffic websites

Robust, easy to implement

Have time to time documentation

Bunch of cool features. 🙂

Installation…

The Cache_Lite class comes courtesy of PEAR, the “PHP Extension and Application Repository” (http://pear.php.net). In case you didn’t know, PEAR is an online repository of free PHP software, including classes and modules for everything from data archiving to XML parsing. When you install PHP, a whole bunch of PEAR modules get installed as well; the Cache_Lite class is one of them.

In case not installed then…

It is just like coding hello world!! 😀

On ubuntu:

sudo aptitude -y update
sudo aptitude install php-pear

Now that we have PEAR, i would use it to install the Cache_Lite extension

sudo pear install Cache_Lite

Perfect! 😀

Checking whether installed/ checking PEAR verson:

Both pear and pecl tools should be available everywhere on command line. For that to work, pear’s binary (bin) directory should be in your PATH variable.

To verify it works, simply type pear. A list of commands should be shown:

The key point is Cache_Lite maintains a unique identifier for every page. Cache_Lite will check for that identifier used before. If so, it will retrieve the stored HTML from disk (can use RAM as turbo charged storage i.e. mount tmpfs in RAM memory) and echo it right away. If not, we:

turn on output buffereing so we can catch all following content

we include the original PHP code

catch the output buffer, and let Cache_Lite store it on disk for the next time.

Like this:

Include Vs Require

include() and require() are slightly different.Basically, include is conditional and require is not.

This would include ‘somefile’ if $something is true:

if($something){

include(“somefile”);

}

This would include ‘somefile’ unconditionally

if($something){

require(“somefile”);

}

This would have VERY strange effects if somefile looked like:

} echo “Ha!I’m here regardless of something: $something<br>n”;

if (false) {

Another interesting example is to consider what will happen if you use include() or require() inside a loop.

$i = 1;

while ($i < 3) {

require(somefile.$i);

$i ;

}

Using require() as above will cause the same file to be used every single iteration.Clearly this is not the intention since the file name should be changing in each iteration of the loop.We need to use include() as below.Include() will be evaluated at each iteration of the loop including somefile.0, somefile.1, etc as expected.

$i = 1;

while ($i < 3) {

include(somefile.$i);

$i ;

}

The only interesting question that remains is what file will be required above.It turns out that PHP uses the value of $i when it reads the require() statement for the first time.So, the require() loop above will include something.1 two times.The include() loop includes something.1 and something.2.

Echo Vs Print

There is a difference between the two, but speed-wise it should be irrelevant which one you use.print() behaves like a function in that you can do:

$ret = print “Hello World”;

and $ret will be 1.

That means that print can be used as part of a more complex expression where echo cannot.print is also part of the precedence table which it needs to be if it is to be used within a complex expression.It is just about at the bottom of the precedence list though.Only “,” AND, OR and XOR are lower.

echo is marginally faster since it doesn’t set a return value if you really want to get down to the nitty gritty.

If the grammar is:

echo expression [, expression[, expression] … ]

Then

echo ( expression, expression )

is not valid.( expression ) reduces to just an expression so this would be valid:

echo (“howdy”),(“partner”);

but you would simply write this as:

echo “howdy”,”partner”;

if you wanted to use two expressions.Putting the brackets in there serves no purpose since there is no operator precedence issue with a single expression like that.