Why Does Drupal put the cached data in the database? It's hard too understand it, because the database can easily cause performance bottleneck.
Drupal core comes out of the box with a good caching system that will handle low level usage. Using the database as cache, you could easily create a site with hundreds/thousands of daily visitors, which is enough for many. This is a good default, as it's easy to setup, but more advanced caching strategies for high performance are supported:
- For both Drupal 6 and 7 there are modules to improve caching by using memcache instead the database for some of the caching.
- For Pressflow 6 and Drupal 7 core you can setup external cache like Varnish.
So Drupal really has a lot of caching possibilities, but in order to use the more advanced for high performance, you need to do some extra work. This will also require various server setup anyways. But making the default easy to use really is preferred.
The question that asked here is not whether the database cache can be replaced by something else, but why Drupal caches database information in the database. A reason that is not mentioned here is that although database information is again cached in the same database, it is not cached in the same format. The original data, such as node information or theme information, is stored in a kind of normalized form, which makes it easy to edit and query data. The cached values are stored in a way that reduces the amount of processing needed to display a page.
Let's say a page view consists of the following simplified steps:
- Fetch information from database
- Manipulate information to make it suitable for display
- Display the result
Now Drupal stores the result of step 2 into the database, so on subsequent page views, steps 1 and 2 do not have to be executed. This saves time and processing power. When the original data changes, the cache needs to be cleared.
Fetching cached data from the database is easy and fast enough because they are simple SQL requests. Also, most of the results are cached in memory by the SQL caching system.
There are also other caching system, but they are not as good as database for a default caching system:
- File system: you have to deal with data coherence, file permission, file lock, file management.
- Memory: is faster than database, but you need extra setup. Thus this one should not be the default caching system.
Well in fact the drupal cache system is pluggable in drupal6 and drupal7, which means that you can put any kind of caching system behind it.
By default drupal uses a database based caching system, but you can replace it for example with the memcache module with a memcache, which scales much better then the database for the cache.
If you want you could even write a file based one, which will probably be must slower.
A very simple explanation of why is that it provides a caching interface which improves performance which by default doesn't add any more layers to a Drupal site.
Other caching mechanisms may be better, faster or whatever but they would introduce additional requirements which would not be needed for many Drupal sites.
Luckily Drupal is made so that the caching system can be swapped for those who need it.