0
votes

Setup: Joomla website on LAMP stack

I have a MySQL table containing some records, these are queried by a simple SELECT on the Joomla article, as pasted below. This specific Joomla website has Caching turned on in Joomla's Global Configuration. I need to randomize the order in which I display the resultset, each time the page is loaded.

Regular php/mysql would offer me two approaches for this: 1. use 'order by RAND()' or any of a number of methods to allow a SELECT query to return reasonably random results. 2. once php gets the result from the SELECT into an array, shuffle the array to get a reasonably random order of array items.

However, as this Joomla instance has Caching turned ON in its Global Configuration, either of the above approaches fails. The first time I load the page the order is randomized, however any further reloads do not cause the order to change, as the page is delivered from cache. The instant the Cache is disabled, both approaches (shuffle/order by rand) work perfectly. What am I missing? How do I override the Global Cache for this specific article? A very simple requirement, that is met by both php and mysql reasonably well, is blocked by the Joomla Cache that I cannot turn off.

The php that returns results from the database.

<pre>
$db = JFactory::getDBO();
$select = "SELECT id FROM jos_mytable;"; //order by RAND()
$db->setQuery($select);
echo $db->getQuery(); //Show me the Query!
$rows = $db->loadObjectList();
//shuffle($rows);
foreach($rows as $row) {
    echo "$row->id";
}
1
I managed to find a workaround that involves hacking Joomla's core. The hack is innocent enough, add an HTML comment to the article you wish to exclude from the cache, then modify the method store() in /libraries/joomla/cache/storage/file.php to look for this comment, and exit the method without adding it to the cache. Ingenious, but still a hack. Via joomlaperformance.com/forums/func,view/catid,8/id,2738Shrinivas

1 Answers

0
votes

I guess you are creating a component or what?

You can consider to clean your component from the cache (perhaps conditionally) after every request:

$cache =& JFactory::getCache('your-component-name');
$cache->clean();