Login   Register  
PHP Classes
elePHPant
Icontem

File: unicache/cache-this.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nikos M.  >  Unicache  >  unicache/cache-this.php  >  Download  
File: unicache/cache-this.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Unicache
Cache data in files, APC or Memcached
Author: By
Last change:
Date: 2012-05-30 12:32
Size: 1,135 bytes
 

Contents

Class file image Download
<?php
// include the unicache configuration
require_once('unicache-config.php');

// user defined function to check per page caching
$unicache_docache=true;
if (
function_exists($unicache_config['cache_func']))
{
    
$unicache_docache=call_user_func($unicache_config['cache_func']);
}

// if caching is enabled
if ($unicache_docache)
{   
    
$cache=UNICACHE_Factory::getCache();

    
$key=$_SERVER['REQUEST_URI'];
    
$ttl=$unicache_config['ttl'];

    
// shutdown function to output and cache
    
function UNICACHE_cacheOutput()
    {
        global 
$cache,$key,$ttl,$unicache_config;
        
        
$webpage ob_get_contents();
        
        
// post cache user defined filter function
        
if (function_exists($unicache_config['filter_func']))
        {
            
$webpage=call_user_func($unicache_config['filter_func'], $webpage);
        }
        
// cache page
        
$cache->put($key,$webpage,$ttl);
        
        
ob_end_clean();
        
// show it first time
        
echo $webpage;
    }

    
// Is cache data still fresh? If so, serve it.   
    
$data=$cache->get($key);
    if (
$data!=false) {
        echo 
$data;
        exit(
0);
    }
    
// start caching
    
ob_start();
    
ob_implicit_flush(false);
    
register_shutdown_function("UNICACHE_cacheOutput");
}
?>