views:

150

answers:

2

I'm setting up a caching system in PHP for database queries.

So when a query like "SELECT * FROM table" is called, it either returns the cached results of that query or the results directly from the DB.

$query = "SELECT * FROM table";
[...]
$data = mysql_query($query);
[...]
fwrite($file,json_encode($data));
[...]

The problem is I'm trying to save the query results to a file and I can't find a textual format that works. I've tried json_encode and serialize, and they both return null or 0. Is there a format that will work for this without having to do mysql_fetch_array() and then serialize?

This is the error I get with json, obviously because I haven't converted the result to an array:

Warning: [json] (php_json_encode) type is unsupported, encoded as null
+1  A: 

why not:

  1. use native mysql cache?
  2. optimize queries so they work fast and don't need to be cached?
zerkms
I'm not really familiar with mysql's built-in cache...Would it give about the same performance increase as what I'm trying to do? I just assumed it wouldn't because the db server still has to be called to return that cache.As for optimizing queries, it's a huge app and things are optimized as much as they can be, but there are still a ton of little DB calls and I'm worried that everything will grind to a halt if we get a traffic spike. I came in when the app was mostly done and I can see many ways to optimize it more, but re-coding everything isn't really an option right now.
start from this page: http://dev.mysql.com/doc/refman/5.1/en/query-cache.html
zerkms
ps: don't forget that with "mega-performance" of cached data you are getting headache of cache invalidation
zerkms
After reading more about mysql's query cache I don't think it will be as effective as doing something on the app-side, considering the size of this app and how it works. Also this caching mechanism I'm trying to set up isn't for all caches, only certain ones. Everything is set up to work, I just need to find a format that will work to save the results to a file. Thanks a lot for your help though :)
@phx-zs: nothing personal, but native cache will be much-much-much-more performant than your solution. just because you cannot answer on your question.
zerkms
I'll give it a shot. I was just intimidated by all the talk in these articles about how it can mess things up if not used properly...
Whoops, looks like mysql's internal cache was already turned on anyway. Also, I ended up making my function work with turning the results into an array, and I ran some timers and between the file I/O and other operations it actually took a fraction longer to run the page than without it.
A: 

It may not be a perfect solution for the issue you are handling, but I have found Justin Vincent's ezSQL Class to be really handy for working with situations like this. The class includes a caching function, which would satisfy the needs you mentioned in the question above. (Bonuses being that it handles alot of the more obtuse PHP<=>MySQL interactions as well.)

Lucanos
Hey, if you are going to bother to downvote me, I would at least appreciate an explanation. If I am wrong, great - educate me!
Lucanos
Wasn't me. Thanks for the suggestion though...I'll keep that class in mind for the next project, but it's too late to incorporate it now.