Web Tool Bag  
Home · Articles · Downloads · Discussion Forum · Web Links · News CategoriesFebruary 05 2012 09:30:03
Navigation
Home
Articles
Downloads
Discussion Forum
Web Links
News Categories
Search
Users Online
Guests Online: 4
No Members Online

Registered Members: 625
Newest Member: nownigcheencex43
Forum Threads
Newest Threads
reverse mortgage
Science channels in ...
How can I see the fu...
Order Flagyl Without...
X-Rumer 7.0 - best s...
Hottest Threads
Installation [11]
Hermoine nude [6]
Captcha picture d... [6]
Integrate with Vi... [5]
Any questions and... [5]
Trusted web hosting

Coupon Code
WEBTOOLBAG
Latest Articles
jQuery Accordion - m...
Few Guidelines for W...
Move OST to PST to U...
Microsoft OST vs. Mi...
Tips for Specialized...
PHP Pear caching

PEAR offers two different packages for caching: Cache and Cache_Lite.
As suggested by the name, Cache_Lite has a lighter design than Cache, and is designed to be faster at the expense of some flexibility and functionality.

Cache_Lite
The Cache_Lite package offers simple, fast, file-based caching. It is restricted to caching in files for speed and simplicity.
Cache_Lite provides three types of caching:

Generic caching of any data

Caching of PHP output

Caching of function return values

The idea behind Cache_Lite is that you only need to load the Cache_Lite class to use it.

It does not load the PEAR class unless needed in a raiseError() call, and not many other classes.

If you are not using a PHP code cache, this package avoids compiling code you potentially will not execute, and keeps latency down.

Example: Output Caching

Following is an example of PHP output caching that serves the entire page from the cache:

===============================================

<?php

require_once "Cache/Lite/Output.php";

$time_s = utime();

if (empty($_GET['id'])) {
    die("please specify an article id!");
}

$cache = new Cache_Lite_Output(
    array('lifeTime' => 300, // 5 minutes
          'cacheDir' => '/tmp/article_cache/'));

if ($cache->start($_GET['id'], 'article')) {
    $cached = true;
} else {
    include_once "DB.php";
    include_once "HTML/Template/Flexy.php";

    $dbh = DB::connect("mysql://test@localhost/test");
    $article = $dbh->getRow(
        "SELECT * FROM articles WHERE id = ?",
        array($_GET['id']), DB_FETCHMODE_OBJECT);

    $dir = dirname(__FILE__);
    $tpl = new HTML_Template_Flexy(
        array('templateDir' => "$dir/templates",
              'compileDir' => "$dir/templates/compiled",
              'filters' => 'Php,SimpleTags,BodyOnly'));
    $tpl->compile('flexy_display_article.tpl');
    $tpl->outputObject($article);

    $cache->end();
    $cached = false;
}

$elapsed = utime() - $time_s;
printf("<div style=\"font-size:x-small\">".
       "(spent %.1fms %s)</div>\n", $elapsed * 1000,
       $cached ? "serving page from cache" : "generating page");

function utime() {
    list($usec, $sec) = explode(" ", microtime());
    return (double)$usec + $sec;
}







===============================================

As you can see, this script only includes Cache/Lite/Output.php every time. If the page is served from a cache, no other code is loaded because DB.php and HTML/Template/Flexy.php are included only if there was no cache hit.

The $cache->start() looks up the requested entry in the cache. If it is found there and has not expired, the cached entry is printed, and the start() method returns TRue.

If a cache entry was not found, start() returns false. Then, the script connects to the database, pulls out the article, compiles a template, and displays the article. After all this, the $cache->end() call prints the output and stores it in the cache.

At the end, the cache output example displays a message to illustrate the response time difference with a cache hit.


Posted by zdravko on June 29 2010 21:19:48 358 Reads · Print
Ratings
Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Login
Username

Password



Not a member yet?
Click here to register.

Forgotten your password?
Request a new one here.
Member Poll
Which PHP framework do you preffer?

Symfony

Zend

PHPDevShell

PHP on TRAX

eZ Components

Fusebox

PhpOpenbiz

Prado

QPHP

Seagull

You must login to vote.
Shoutbox
You must login to post a message.

Vince
03/10/2011 18:17
Hi, How to remove Register from Login screen? I don't want them to register and have full access! if you leave register then they should not have any rights until the admin assigns them

webtoolz
26/09/2011 08:28
Please describe your problem with more details. Thank you.

bimmer98
22/11/2010 18:31
Help. There was a problem with the request; error regarding feedbackzdr form program

PHP Obfuscator
Copyright © 2012 - www.webtoolbag.com