Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same MySQL query with all users without having to reconnect to the database [closed]

I'll try to be simple, so what i need is, perform a MySQL query (selecting records from a table) let's say ill produce the query with cron jobs or whatever every 5 minutes, and save this query or results to a file or a global variable if it's possible, and then serving it to all clients (i.e. using the same query to all users, without having to reconnect to that database.) Now i know some of you will tell me to use cache, but i searched for it, and if the table changes there will be new cache, and my table is constantly changing, now i don't care if the table has changed, i just want to serve the same query to all clients. Is that possible? your recommendations please. Thanks.

The cache expires automatically when the table is modified (inserts, updates, delete's, etc). And my table is constantly changing, so is it possible to save a cache even if the table has been modified? or should i look for a different solution?

like image 249
user1852196 Avatar asked Nov 26 '12 02:11

user1852196


1 Answers

If the result set isn't too big, you could cache it into a generated script like so:

file_put_contents(
    'mycache.php', 
    '<?php return ' . var_export($results, true) . ' ?>'
);

Then to use it:

$results = include 'mycache.php';
like image 70
Ja͢ck Avatar answered Sep 28 '22 18:09

Ja͢ck