Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Im looking for a example usage of xcache with database

posted by o 10 years ago

Hi, Im using fDatabase and fResult in my production.

I want to store all result in xcache with 120 seconds and retrieve. How can i make this ?

(This is an old question, but I wanted to provide somewhat of an answer for future visitors.)

The following should do what you want:

$cache    = new fCache('xcache');

$sql      = $db->escape("SELECT foo, bar FROM baz WHERE id = %i", $id);
$sql_hash = md5($sql);

if (!$data = $cache->get($sql_hash)) {
    $data = $db->query($sql)->fetchAllRows();
    $cache->set($sql_hash, $data, 120);
}

// Use the result

If you are looking to do this for all results, you are probably going to want to write a helper class. Using fDatabase::escape() creates a fully-formed SQL statements that includes all values. This is essential so you get a different hash for different IDs.

If you are doing this for a lot of records you are going to want to keep an eye on the usage of xcache.

posted by wbond 8 years ago