Links: dibi | API reference

Forum: [česky] [english]

dibi forum

tiny ‘n’ smart
database layer

You are not logged in.

#1 2 years ago

gotisch
Member
Registered: 2009-05-23
Posts: 3

feature request: cache

Hello,

is there a planned feature to add cache for queries?

something like

$connection->querywithcache(“thequery”,1800);

to query database only every 1800 seconds and else return cached values?
i solved it currently with

<?php
$query = "SELECT ach.achievement AS achievementID, from_unixtime(ach.date) as achievementTime, g.name AS guildname,g.guildId,`c`.*,a.locked AS accbanned,a.gmlevel,a.username,gr.rname FROM [:chars:character_achievement] ach
                                LEFT JOIN [:chars:characters] AS c ON c.guid=ach.guid
                                LEFT JOIN [:chars:guild_member] AS gd ON c.guid=gd.guid
                                LEFT JOIN [:chars:guild_rank] AS gr ON gr.guildid=gd.guildid AND gr.rid=gd.rank
                                LEFT JOIN [:chars:guild] AS g ON gd.guildid = g.guildId
                                LEFT JOIN [:realm:account] AS a ON a.id=c.account
                                WHERE ach.date > ( UNIX_TIMESTAMP( ) - ( 3 *24 *60 *60 ) )
                                ORDER BY ach.date DESC LIMIT 0,15";
        $filename = "/tmp/dbcache/" . md5($query);
        // check cache:
        $limit = 300; // limit of 5 min
        if (file_exists($filename) && filemtime($filename) > time()-$limit) {
                $all = unserialize(file_get_contents($filename));
        } else {
                $result = $connection->query($query);
                $all = $result->fetchAll();
                if (!file_exists("/tmp/dbcache/"))
                        mkdir("/tmp/dbcache/", 0777);
                if($fp = fopen($filename, 'w')) {
                        fwrite($fp, serialize($all));
                        fclose($fp);
                }
        }
?>

having dibi do it automagically (if wanted) would be really cool.

Last edited by gotisch (2010-03-07 22:52)

 

Board footer