Page 1 of 1

Creating cache files

Posted: Wed Feb 09, 2011 8:34 pm
by hah
Hello, how can I create a custom cache file, to store information? Not only in data_global.php, because the info is a lot.

Re: Creating cache files

Posted: Thu Feb 10, 2011 8:04 am
by AmigoJack
Just create your own method in /includes/cache.php which might look like this:

Code: Select all

    function obtain_my_stuff() {
        // Unique name suffix for the cache file
        define( MY_STUFF, '_my_stuff' );

        // Not cached already? Then get it
        if( ( $vData= $this-> get( MY_STUFF ) )=== false ) {
            global $db;

            // vData can have any format, of course. Doesn't need to be an array
            $vData= array();

            // As an example, we just use all rows from a database table as data
            $sql= 'SELECT * FROM '. ANY_TABLE;
            $result= $db-> sql_query( $sql );
            while( $aRow= $db-> sql_fetchrow( $result ) ) $vData[]= $aRow;
            $db-> sql_freeresult( $result );

            // Create or update cache file. The third parameter is optional and defines
            // the time to live in seconds. 31536000 = 60 * 60 * 24 * 365 = 1 year. By
            // default, a cache file lives for 1 year.
            $this-> put( MY_STUFF, $vData, 31536000 );
        }

        return $vData;
    } 
So anywhere you then could just use:

Code: Select all

$vMyStuff= $cache-> obtain_my_stuff(); 

Re: Creating cache files

Posted: Thu Feb 10, 2011 2:14 pm
by hah
Thanks. :)