Creating cache files

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
hah
Registered User
Posts: 85
Joined: Sat Feb 13, 2010 11:49 am
Location: Pernik, Bulgaria
Name: Alex Georgiev
Contact:

Creating cache files

Post by hah » Wed Feb 09, 2011 8:34 pm

Hello, how can I create a custom cache file, to store information? Not only in data_global.php, because the info is a lot.

User avatar
AmigoJack
Registered User
Posts: 5615
Joined: Tue Jun 15, 2010 11:33 am
Location: グリーン ヒル ゾーン
Contact:

Re: Creating cache files

Post by AmigoJack » Thu Feb 10, 2011 8:04 am

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(); 
The worst thing about censorship is ███████████
Affin wrote:
Tue Nov 20, 2018 9:51 am
The problem is probably not my English but you do not want to understand correctly.
...
We will not come anybody anyway, nevertheless, it's best to shit this.

User avatar
hah
Registered User
Posts: 85
Joined: Sat Feb 13, 2010 11:49 am
Location: Pernik, Bulgaria
Name: Alex Georgiev
Contact:

Re: Creating cache files

Post by hah » Thu Feb 10, 2011 2:14 pm

Thanks. :)

Locked

Return to “[3.0.x] MOD Writers Discussion”