开发者

cache the database result in txt file and how to output it?

开发者 https://www.devze.com 2023-02-20 03:04 出处:网络
eg: $results = mysql_query(\'SELECT title FROM articl开发者_高级运维e\'); now I want to put the result in a text file. Then :

eg: $results = mysql_query('SELECT title FROM articl开发者_高级运维e'); now I want to put the result in a text file. Then :

 $filepath = 'test.txt';
 if (is_file($filepath)) {
   echo file_get_contents($filepath);
 } else {
   // read the databse, and stored the result to the test.txt.
 }
  1. How to wirite the else part?
  2. I want to echo the content in list style. how should I do?

Thank you


I really wouldn't.

Your filesystem 'cache' is likely to be slower than the database, and introduces extra bonus consistency problems on top of the added complexity.

If you have expensive, complex queries, look at putting the cached results in another database table, or memcache, or optimising/denormalising the database to make the query faster.


I would suggest you look at an opcode cache such as APC. This will handle the cache expiry for you, and will be significantly faster than either this filesystem cache or a SQL query.

I often use this technique handle slow queries, from read often, write little databases.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号