Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Class: Cache_Output

Source Location: /Cache-1.5.7/Cache/Output.php

Class Overview

PEAR
   |
   --Cache
      |
      --Cache_Output

Class to cache the output of a script using the output buffering functions


Author(s):

Version:

  • $ID: $

Variables

Methods


Child classes:

Cache_OutputCompression
Cache using Output Buffering and contnet (gz) compression.

Inherited Variables

Inherited Methods

Class: Cache

Cache::Cache()
this Constructor set the Container Property as a Cache_Container|Cache_Error Object.
Cache::extSave()
Stores a dataset with additional userdefined data.
Cache::flush()
Flushes the cache - removes all data from it
Cache::garbageCollection()
Calls the garbage collector of the storage object with a certain probability
Cache::generateID()
Generates a "unique" ID for the given value
Cache::get()
Returns the requested dataset it if exists and is not expired
Cache::getCaching()
Returns the current caching state.
Cache::getUserdata()
Returns the userdata field of a cached data set.
Cache::isCached()
Checks if a dataset exists.
Cache::isExpired()
Checks if a dataset is expired
Cache::load()
Loads the given ID from the cache.
Cache::remove()
Removes the specified dataset from the cache.
Cache::save()
Stores the given data in the cache.
Cache::setCaching()
Enables or disables caching.
Cache::_Cache()

Class Details

[line 97]
Class to cache the output of a script using the output buffering functions

Simple output cache. Some pages require lots of time to compute. Caching the output can increase the overall speed dramatically, especially if you use a Shared Memory storage container.

As you can see in the example the usage is extemely simple. To cache a script simple put some few lines of code in front of your script and some at the end. A preferrable place for this are the auto_prepend and auto_append files (=> php.ini).

Usage example:

// place this somewhere in a central config file define(CACHE_STORAGE_CLASS, 'file'); // file storage needs a dir to put the cache files define(CACHE_DIR, '/var/tmp/');

// get a cache object $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));

// compute the unique handle. // if your script depends on Cookie and HTTP Post data as well // you should use: // $cache_handle = array( // 'file' => $REQUEST_URI, // 'post' => $HTTP_POST_VARS, // 'cookie' => $HTTP_COOKIE_VARS // ); // But be warned, using all GET or POST Variables as a seed // can be used for a DOS attack. Calling http://www.example.com/example.php?whatever // where whatever is a random text might be used to flood your cache. $cache_handle = $cache->generateID($REQUEST_URI);

// now the magic happens: if cached call die() // to end the time consumptiong script script execution and use the cached value! if ($content = $cache->start($cache_handle)) { print $content; print '<p>Cache hit</p>'; die(); }

// time consumption script goes here.

// store the output of the cache into the cache and print the output. print $cache->end(); print "<p>Cache miss, stored using the ID '$id'.</p>";

If you do not want to cache a whole page - no problem:

if (!($content = $cache->start($cache_handle))) { // do the computation here print $cache->end() } else { print $content; }

If you need an example script check the (auto_)prepend and (auto_)append files of my homepage:

http://www.ulf-wendel.de/php/show_source.php?file=prepend http://www.ulf-wendel.de/php/show_source.php?file=append

Don't know how to use it or you need profiling informations?` Ask Christian he was patient with me and he'll be so with your questions ;).

Have fun!



[ Top ]


Class Variables

$output_group =  ''

[line 114]

Group passed to start()

Type:   string


[ Top ]

$output_id =  ''

[line 106]

ID passed to start()

Type:   string


[ Top ]



Method Detail

_Cache_Output (Destructor)   [line 120]

void _Cache_Output( )

PEAR-Deconstructor

Call deconstructor of parent


[ Top ]

end   [line 163]

void end( [ $expire = 0], [ $userdata = ''])


Overridden in child classes as:

Cache_OutputCompression::end()
Stops the output buffering, saves it to the cache and returns the _compressed_ content.

Parameters:

   $expire     
   $userdata     

[ Top ]

endGet   [line 210]

string endGet( )

Returns the content of the output buffer but does not store it into the cache.

Use this method if the content of your script is markup (XML) that has to be parsed/converted (XSLT) before you can output and store it into the cache using save().


[ Top ]

endPrint   [line 180]

void endPrint( [ $expire = 0], [ $userdata = ''])

Stores the content of the output buffer into the cache and prints the content.

Overridden in child classes as:

Cache_OutputCompression::endPrint()

Parameters:

   $expire     
   $userdata     

[ Top ]

printContent   [line 192]

void printContent( [string $content = ''])

Sends the data to the user.

This is for compatibility with OutputCompression

  • Access: public

Overridden in child classes as:

Cache_OutputCompression::printContent()
Sends the compressed data to the user.

Parameters:

string   $content     

[ Top ]

start   [line 133]

string start( string $id, [string $group = 'default'])

starts the output buffering and returns an empty string or returns the cached output from the cache.
  • Access: public

Parameters:

string   $id     dataset ID
string   $group     cache group

[ Top ]


Documentation generated on Thu, 15 Sep 2011 07:00:06 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.