Minimize database interactions with caching
I was looking at this:
http://dev.mysql.com/doc/refman/5.1/en/query-cache.html
It's an amazing way of caching stuff, but it happens on the database side, it would be great if there was some kind of code for PHP that would do the same for you.
I mean you would send your queries开发者_Python百科 to that intermediary layer, it would look at your queries, decides if it needs to connect to the db again or not.
Has anyone made anything like that for PHP? Do you think it's a good idea?
I wrote a blog post that goes into a lot of details on this topic at http://www.joeyrivera.com/2009/caching-using-phpzend_cache-and-mysql/. It talks about why you should cache and how to implement caching with zend cache. Communicating with the db is usually slow so you should always cache when possible. Caching to ram will net you greater performance results as ram is faster than the file system using something like memcache but if you don't have that available then just use file system caching instead.
For any caching it is important that the actual caching and invalidation are done by the same application (e.g. the sql-server).
While it probably isn't difficult to write some client-side query-cache this does absolutely make no sense in most cases as the client-cache would not be notified (by the sql-server) when the cached data becomes outdated. But actually the query-cache is that layer you suggest.
As mysql's query-cache doesn't know anything about your application-logic, it has to invalidate the cache by other criteria (e.g. the table changed). This can result in poor performance if used as the only cache (especially on fast-changing tables).
Therefore you should use some sophisticated caching engine on your client with respect to your application logik.
精彩评论