2

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;

3
  • What exactly is dynamic about the stylesheet that requires you to check and see if the css needs to be refreshed? Commented Dec 26, 2012 at 19:40
  • Expires is a strong caching mechanism so you will have a hard time fixing problems with it if you also don't implement some sort of cache busting.
    – user188654
    Commented Dec 26, 2012 at 19:44
  • This might help stackoverflow.com/questions/3777974/…
    – kodeart
    Commented Dec 26, 2012 at 19:48

5 Answers 5

2

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;
0

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

0

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

0

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
    }
?>
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.