Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The most simple way to cache MySQL query results using PHP?

Tags:

php

mysql

caching

Each time someone lands in my page list.php?id=xxxxx it requeries some MySQL queries to return this:

$ids = array(..,..,..); // not big array - not longer then 50 number records
$thumbs = array(..,..,..); // not big array - not longer then 50 text records
$artdesc = "some text not very long"; // text field

Because the database from which I make the queries is quite big I would like to cache this results for 24h in maybe a file like: xxxxx.php in a /cache/ directory so i can use it in include("xxxxx.php") if it is present. ( or txt files !? , or any other way )

Because there is very simple data I believe it can be done using a few of PHP lines and no need to use memcached or other professional objects.

Becasuse my PHP is very limited can someone just place the PHP main lines ( or code ) for this task ?

I really would be very thankfull !

like image 645
Pedro P Avatar asked Apr 01 '13 15:04

Pedro P


People also ask

How do I query a cache in PHP?

We can use the file_put_contents() function to take the results of our database query (stored in the $rows variable) and save it to the cache. txt file. The file_put_contents() PHP function is identical to calling fopen() , fwrite() and fclose() successively to write data to a file. Read more here.

Does MySQL have cache query results?

The MySQL query cache is a query results cache. It compares incoming queries that start with SEL to a hash table, and if there is a match returns the results from the previous execution of the query. There are some restrictions: The query must match byte-for-byte (the query cache avoids parsing)

How do you cache a query?

To cache a query, go ahead and save the query first. Fig 1: Press the button to "Save" the query. Then, to cache your most important queries select the “Enable Caching” checkbox and enter a refresh rate.


2 Answers

Caching a PHP array is pretty easy:

file_put_contents($path, '<?php return '.var_export($my_array,true).';?>');

Then you can read it back out:

if (file_exists($path)) $my_array = include($path);

You might also want to look into ADOdb, which provides caching internally.

like image 118
Adrian Avatar answered Oct 10 '22 05:10

Adrian


Try using serialize;

Suppose you get your data in two arrays $array1 and $array2. Now what you have to do is store these arrays in file. Storing string (the third variable in your question) to file is easy, but to store an array you have to first convert it to string.

$string_of_array1 = serialize( $array1 );
$string_of_array2 = serialize( $array2 );

The next problem is the naming of cache files so that you can easily check if the relevant array is already available in cache. The best way to do this is to create an MD5 hash of your mysql query and use it as cache file name.

$cache_dir = '/path/cache/';

$query1 = 'SELECT many , fields FROM first_table INNER JOIN another_table ...';
$cache1_filename = md5( $query1 );

if( file_exists( $cache_dir . $cache1_filename ) )
{
    if( filemtime( $cache_dir . $cache1_filename ) > ( time( ) - 60 * 60 * 24 ) )
    {
        $array1 = unserialize( file_get_contents( $cache_dir . $cache1_filename ) );
    }
}

if( !isset( $array1 ) )
{
    $array1 = run_mysql_query( $query1 );
    file_put_contents( serialize( $array1 ) );
}

Repeat the above with the other array that should be stored in a separate file with MD5 of the second query used as the name of second cache file.

In the end, you have to decide how long your cache should remain valid. For the same query, there may change records in your mysql table that may make your file system cache outdated. So, you cannot just rely on unique file names for unique queries.

Important:

  • Old cache files have to be deleted. You may have to write a routine that checks all files of a directory and deletes the files older than n seconds.
  • Keep the cache dir outside the webroot.
like image 32
Hamid Sarfraz Avatar answered Oct 10 '22 06:10

Hamid Sarfraz