Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a SQL query (count) every 30 sec, and then save the output to some file

Tags:

file

sql

php

I am developing a website and got a database where people can insert data (votes). I want to keep a counter in the header like "x" votes have been cast. But it is possible that there will be a lot of traffic on the website soon. Now I can do it with the query

SELECT COUNT(*) FROM `tblvotes

and then display number in the header, but then every time the users changes page, it will redo the query, so I am thinking, maybe it is better to the query once every 30 sec (so much less load on the mysql server) but then I need to save the output of it to some place (this shouldn't be so hard; I can write it to a textfile?) But how can I let my website automatically every 30 sec run the query and put the number in the file. I got no SSH to the server so I can t crontab it?

If there is something you might not understand feel free to ask!

like image 698
Maximc Avatar asked Dec 21 '22 22:12

Maximc


1 Answers

Simplest approach: Write the result into a local textfile, check the filetime of the textfile on every request to be less than now() + 30 seconds, and if so, update the file. To update, you should lock the file. While the file is being updated, other users for whom the condition now() + 30 is met should only read the currently existing file to avoid race conditions. Hope that helps, Stefan

like image 196
ExternalUse Avatar answered Dec 29 '22 11:12

ExternalUse