Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache only part of a page in PHP

Tags:

php

caching

Is it possible to cache only a specific part of a page in PHP, or the output of a specific section of code in the PHP script? It seems when I try to cache a particular page, it caches the whole page which is not want I want, some of the content in my page should be updated with every page load while others (such as a dropdown list with data from a database) only needs to be updated every hour or so.

like image 216
emkay Avatar asked Aug 31 '11 08:08

emkay


3 Answers

If you are talking about caching by the browser (and any proxies it might interact with), then no. Caching only takes place on complete HTTP resources (i.e. on a per URI basis).

Within your own application, you can cache data so you don't need to (for example) hit the database on every request. Memcached is a popular way to do this.

like image 169
Quentin Avatar answered Sep 18 '22 10:09

Quentin


Zend_Cache

I would probably use Zend Frameworks Zend_Cache library for this.

You can just use this component without needing to use the entire framework.

Step over to Zend Framework Download Page and grab the latest.

After you have downloaded the core files, you will need to include Zend_Cache in your project. Zend_Cache docs.

Have you decided how you want to cache your data? Are you using a file system? Or are you memcache? Once you know which you are going to use, you need to use a specific Zend_Cache backend.

Zend_Cache Backends / Zend_Cache Frontends

  • You need to use a backend (how you are caching in storage what it is you want to cache) and
  • You need to use a frontend (how do you actually want to cache.. like using a buffer, or caching function results etc)

Backend documentation: Zend_Cache Backends Frontend documentation: Zend_Cache Frontends

So you would do something like this...

<?php
// configure caching backend strategy
$backend = new Zend_Cache_Backend_Memcached(
    array(
        'servers' => array( array(
            'host' => '127.0.0.1',
            'port' => '11211'
        ) ),
        'compression' => true
) );

// configure caching frontend strategy
$frontend = new Zend_Cache_Frontend_Output(
    array(
        'caching' => true,
        'cache_id_prefix' => 'myApp',
        'write_control' => true,
        'automatic_serialization' => true,
        'ignore_user_abort' => true
    ) );

// build a caching object
$cache = Zend_Cache::factory( $frontend, $backend );

This would create a cache which makes use of the Zend_Cache_Frontend_Output caching mechanisms.

To use Zend_Cache_Frontend_Output which is want you want, it would be simple. Instead of the core you would use output. The options which you pass are identical. Then to use it you would:

Zend_Cache_Frontend_Output - Usage

// if it is a cache miss, output buffering is triggered
if (!($cache->start('mypage'))) {

    // output everything as usual
    echo 'Hello world! ';
    echo 'This is cached ('.time().') ';

    $cache->end(); // output buffering ends

}

echo 'This is never cached ('.time().').';

Useful Blog: http://perevodik.net/en/posts/14/

Sorry this question took longer to write than expected and lots of answers have been written I see!

like image 20
Layke Avatar answered Sep 19 '22 10:09

Layke


You could roll your own caching with ob_start(), ob_end_flush() and similar functions. Gather the desired output, dump it into some file or database, and read later if conditions are the same. I usually build md5 sum of the state and restore it later.

like image 37
Milan Babuškov Avatar answered Sep 22 '22 10:09

Milan Babuškov