By default, Cache_DB creates its cache files in a
subdirectory of the current directory called
db_query. You can change this by passing a
directory name as part of an options array as a second argument to
the Cache_DB constructor. This sets the cache
directory to /tmp/db_query:
$cache = new Cache_DB('file',array('cache_dir' => '/tmp/'));
The first argument, file, tells
Cache_DB what container to use to store the cached
data. file is the default, but you need to include
it here to specify the container options in the second argument. The
relevant container option is cache_dir, which
tells Cache_DB where to create the
db_query subdirectory. Including a trailing
slash is required.
By default, entries stay in the cache for one hour. You can adjust
this by passing a different value (in seconds) when creating a new
Cache_DB object. Here's how to
keep entries in the cache for one day, 86,400 seconds:
$cache = new Cache_DB('file',array('cache_dir' => '.',
'filename_prefix' => 'query_'),86400);
Because the expiration time is the third argument, you have to pass
the defaults for the first two arguments as well.
$cache->flush('db_cache');
It's very important to include the
db_cache argument to flush( ).
The PEAR Cache system supports dividing up the
cached items into different groups, and the
Cache_DB object puts everything
it's keeping track of in the
db_cache group. Leaving out the group argument
results in deleting the files in the base cache directory (which is
probably the directory you're running your script
from).
The file container stores each result in a file whose name is based
on an MD5 hash of the query that generated the particular result.
Because MD5 is case-sensitive, the file container is case-sensitive,
too. This means that if the results of SELECT
* FROM
zodiac are in the cache, and you run the query
SELECT *
from zodiac, the results
aren't found in the cache, and the query is run
again. Maintaining consistent capitalization, spacing, and field
ordering when constructing your SQL queries results in more efficient
cache usage.