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 !
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.
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)
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With