Caching with PEAR::Cache - Defining the Cache
(Page 3 of 6 )
Pear::Cache is flexible in that it can cache script results in various ways, such as databases, files and even shared memory. We will pass two arguments when we create an instance of the cache object: the storage type and an array of options.
Valid storage options are: db, dbx, file, mdb, msession, phplib and shm. For this tutorial we will be storing our cached results as files.
Each storage mechanism accepts different parameters. The valid options for storing cached data as files are: cache_dir, filename_prefix and max_userdata_linelength.
The cache_dir option enables us to specify the directory where we want our cached files to be stored.
The filename_prefix enables us to specify a prefix to add to each cached file generated. This allows us to keep our cache organized while still keeping all cache files in the same directory. It also provides us a way to work around any clashing IDs when we use the generateID method discussed later.
The max_userdata_linelength is used to specify how much the class' fgets function will read at a time when retrieving cached data. max_userdata_linelength is used primarily for the sake of efficiency.
While of course you're welcome to experiment with the various options, we won't use filename_prefix or max_userdata_linelength. We’ll keep things simple and set just the directory used to house our cached files.
Now we can now instantiate the class:
<?php $store_as = "file"; $options = array("cache_dir" => getcwd() . "/cache"); $cache = new cache($store_as, $options); ?> |
Next: Implementing a Framework >>
More PEAR Articles Articles
More By bluephoenix