Class: Cache
- Package: saf.Cache
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 1.2, 2003-06-28, $Id: Cache.php,v 1.5 2007/10/06 00:06:30 lux Exp $
- Access: public
Cache provides a simple and lightweight system for caching dynamic web
pages. Offers ordinary filesystem caching, as well as caching to Berkeley
Databases (BDB) (good for persistent objects), and optionally to a proxy server.
In the proxy server situation, Cache does not actually store the data, instead,
Cache simply passes an HTTP Expires header, setting a time for the proxy server
to cache for us. Can optionally use the saf.File.Store package to improve the
performance of filesystem-based caching on large volume stores.
New in 1.2:
- Added a shutdown() method.
New in 1.4:
- New logic in is_cacheable() allows subsequent rules to override previous ones.
Usage Example
<?php
$cache = new Cache ("cache");
// set cache to expire every 15 minutes
if ($cache->expired ($REQUEST_URI, 900)) {
// re-cache the page
ob_start ();
// go about creating the page
// now we need to send the data to the cache file
// as well as to the visitor
$data = ob_get_contents ();
ob_end_clean ();
$cache->file ($REQUEST_URI, $data);
echo $data;
} else {
// show the cached version
echo $cache->show ($REQUEST_URI);
}
?>
Return to Top
Properties
$dir
Contains the path to the directory used to store the cached pages.
$set
Stores whether the URI was determined cacheable.
$error
Contains the error message if an error occurs.
Return to Top
Methods
Cache ($dir = '')
Constructor method. Defines the directory Cache will use to store pages.
If the string 'mod:proxy' is given instead, Cache will know not to cache the data
locally, but rather to simply pass an HTTP Expires header for the proxy server to
do the work. If $dir is a string beginning with 'bdb:', Cache will store the data
in a Berkeley Database identified by the rest of the value of $dir. If the $dir
string begins with 'store:', Cache will use the saf.File.Store package to store
cached documents, which is recommended over the default file handling for caches
with a larger number of documents. See that package for more details.
setIgnoreChars ($ignore = 0)
Sets the number of characters to ignore when using the 'store:' data
store. This allows for better distribution of files across folders by
ignoring a specified number of characters at the beginning of the file name.
Note that the serialize() method will be called before the characters are
evaluated, so any non-valid characters will be stripped out and should not
be counted in this number.
serialize ($file = '')
- Access: public
- Return: string
Takes the string provided (which is usually the global $REQUEST_URI,
and returns a version of it without illegal characters so it can be used as a
file name to store a cached page.
file ($file = '', $data = '')
- Access: public
- Return: boolean
Takes a string (to serialize into a file name) and page data to be
cached, and creates a cached file that can be recalled later. Also serves as
the caching call in BDB and proxy server caching situations. Note: For BDB
data stores, you will want to call $cache_obj->bdb->close (); after you have
finished caching, so that BDB doesn't lock the database on you for the next
user.
expired ($file = '', $duration = 0)
- Access: public
- Return: boolean
Takes a string (to serialize into a file name) and a duration (the number
of seconds to keep the cached file for), and returns whether or not the file has
expired. True means it has.
expire ($file)
Cause a single cached file to expire so it can be regenerated on the next
request for it.
show ($file = '')
- Access: public
- Return: string
Takes a string (to serialize into a file name), and returns its contents
in a string (for the purpose of passing on to the visitor).
is_cacheable ($uri, $list)
- Access: public
- Return: boolean
Checks a URI against a list of cacheable pages, and returns a true/false
as to whether or not that URI is cacheable. Pages in the cacheable list may
include a wildcard (*) character to imply any number of characters.
shutdown ()
Performs any possible shutdown logic, depending on the cache
storage method.
clear ()
Flush the entire cache at once so that all files will be regenerated
on their next request.
Return to Top