haris
haris

Reputation: 3875

PHP Simple Caching

I have a dynamic stylesheet generated from the following code.

Which is the best way to cache this to a file and load it if the file exists.

case 'stylesheet':
    header('Content-type: text/css');   
    header("Cache-Control: must-revalidate"); 
    $offset = 72000 ; 
    $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"; 
    header($ExpStr);

    $stylesheets = array(
        'open_sans'   => file_get_contents('http://fonts.googleapis.com/css?family=Open+Sans:400,600,700')
    );
    exit;

printed from echo CSS {$stylesheets['open_sans']} CSS;

Upvotes: 2

Views: 3784

Answers (5)

Lix
Lix

Reputation: 162

I suggest you APC cache. It's a stable and fast memory cache for PHP. I use it for my whole app but you can use it just to save a variable if you want. You can save the contents of $stylesheets['open_sans'](using apc_store function) in a cache variable and then is going to be served from there(you will use apc_fetch), from RAM memory(fast), for as long as you want setting the expiration time.

Upvotes: 0

Ken Le
Ken Le

Reputation: 1805

Try phpFastCache.com

This is example, simple. Caching your CSS can do like this:

require_once("phpfastcache/phpfastcache.php");

$css = __c()->get("csspage.user_id_something");

if($css == null) {
       // handle your css function here
       $css = "your handle function here";
       // write to cache 1 hour
       __c()->set("csspage.user_id_something", $css, 3600);
}

echo $css;

PHP Cache whole web page: You can use phpFastCache to cache the whole webpage easy too. This is simple example, but in real code, you should split it to 2 files: cache_start.php and cache_end.php. The cache_start.php will store the beginning code until ob_start(); and the cache_end.php will start from GET HTML WEBPAGE. Then, your index.php will include cache_start.php on beginning and cache_end.php at the end of file.

<?php
    // use Files Cache for Whole Page / Widget

    // keyword = Webpage_URL
    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    $html = __c("files")->get($keyword_webpage);

    if($html == null) {
        ob_start();
        /*
            ALL OF YOUR CODE GO HERE
            RENDER YOUR PAGE, DB QUERY, WHATEVER
        */

        // GET HTML WEBPAGE
        $html = ob_get_contents();
        // Save to Cache 30 minutes
        __c("files")->set($keyword_webpage,$html, 1800);
    }

    echo $html;
?>

Reduce Database Calls PHP Caching Class For Database : Your website have 10,000 visitors who are online, and your dynamic page have to send 10,000 same queries to database on every page load. With phpFastCache, your page only send 1 query to DB, and use the cache to serve 9,999 other visitors.

<?php
    // In your config file
    include("phpfastcache/phpfastcache.php");
    phpFastCache::setup("storage","auto");

    // phpFastCache support "apc", "memcache", "memcached", "wincache" ,"files", "sqlite" and "xcache"
    // You don't need to change your code when you change your caching system. Or simple keep it auto
    $cache = phpFastCache();

    // In your Class, Functions, PHP Pages
    // try to get from Cache first.
    // product_page = YOUR Identity Keyword
    $products = $cache->product_page;

    if($products == null) {
        $products = YOUR DB QUERIES || GET_PRODUCTS_FUNCTION;


   // set products in to cache in 600 seconds = 10 minutes
        $cache->product_page  = array($products,600);
    }

    foreach($products as $product) {
        // Output Your Contents HERE
    }
?>

Upvotes: 0

WebStylePress
WebStylePress

Reputation: 1046

Why not cache whole website instead of just the CSS file. Check out this Cache.

Upvotes: 0

haris
haris

Reputation: 3875

I think I am good now.

I found a very simple and powerful class: https://github.com/gilbitron/PHP-SimpleCache

And for checking I am doing:

if (file_exists(CACHE_PATH . 'stylesheet.cache')) {
    require CACHE_PATH . 'stylesheet.cache'; 
    exit;

Upvotes: 2

Ian Gregory
Ian Gregory

Reputation: 5820

There's a simple cache implementation here that could be used in code like yours.

Upvotes: 0

Related Questions