|Frames||Modems||Help||Home Page||Chipsets||Search||No Frames|
|Diary Entries||See also Site Info & Diary.|
|29 April 2004||More free gifts: a Cache_Lite Maintenance class...|
This little page is part 2 in a Cache_Lite case-study, and offers a Maintenance class that extends the usefulness of Cache_Lite. It will be of interest to anyone that uses the PHP scripting language. Part 1 concerns the process of reducing the page-preparation time.
After successfully reducing the page-preparation time of the chips pages by 90%, it was now time to turn to the back-of-office part of the job. If the original SQL tables change, then any cached data that depends on them needs cleaning so that caching starts afresh. In addition, I wanted a way to be able to keep an eye on the size and numbers inside the cache. The Cache_Lite_Maintain class (trumpet fanfare) fulfills both purposes.
The following paragraphs will mention these previous diary entries:
..and the Class can be downloaded here:
Pages on modem-help.com are produced from a series of SQL tables. The target for each page is that the page-processing completes in less than 0.1 second. Caching is used on most search pages in order to achieve this target. If the data in the tables changes, then all cached files that depend on this data need to be removed (clean()-ed).
Up to this point, I’d inserted the clean() statements one-by-one into the correct places in the maintenance-scripts that performed the SQL table updating. This was fine when there was little caching and few tables affected. My confidence in caching had grown, however, and it’s use looked likely to grow on my site. In this scenario, the web of interdependencies between cache groups and tables was going to increase exponentially and rapidly become unmanageable. The Cache_Lite_Maintain class was written to sort this, and the concept of tainting is the key to comprehending it.
Here is the overview:
Cache_Lite organises a cache into 3 levels:
1 directory - top level 2 group - second level 3 id - third level
..and one of the key understandings with Cache_Lite is to make sure that at least the combination of all 3 is unique. I’ve chosen to make id unique (based on the SQL query, see 26 Apr 2004 for how) and to use group as the means of clean()-ing the cache.
Let’s decide that all cached files will reside in a single directory, and we are then dealing with just a single collection of files which can be sifted into unique groups. On my site these groups refer to search pages; so far there are three: chips, names and EEPROMs. As id depends on the SQL query, that means that there is a 2D-matrix of dependances, with group as one axis and the SQL tables as another:
group/table table1 table2 table3 ... tablen group1 xx .. xx ... xx group2 .. .. xx ... xx group3 .. .. .. ... xx ... ... ... ... ... ... groupn .. xx .. ... xx
So, in the example as above, if tablen changes all groups will need clean()-ing, whereas if table2 changes only groupn will need removing. To restate this in the language of the Cache_Lite_Maintain class, “if tablen changes all groups become tainted, whereas if table2 changes only groupn will become tainted”. When the Cache_Lite_Maintain::clean() function is called, all tainted groups are cleaned.
in the first case: $groups = array( group1, group2, ... groupn ); $cache = Cache_Lite_Maintain( $options, $groups ); in the second case: $connects = array( group1 => array( table1, table3, ... , tablen ), group2 => array( table3, ... , tablen ), ... groupn => array( table2, ... , tablen ) ); $cache = Cache_Lite_Maintain( $options, $connects ); ($options is the same array as in the Cache_Lite constructor.)
in the first case: $groups = array( group1, group2, ... groupn ); $tainted = $cache -> setTainted( $groups ); in the second case: $tables = array( table1, table2, ... tablen ); $tainted = $cache -> taint( $tables ); // still taints groups, not tables
in all cases: $groups = $cache -> clean(); // removes all tainted groups
The final item is getStats() which will return an array containing numbers, sizes & oldest dates for all tainted groups + the disk-free space in bytes.
$cache->setTainted($groups); $stats = $cache->getStats(); $groupNumber = $stats[ $group ]['number']; $groupSize = $stats[ $group ]['size']; $groupTime = $stats[ $group ]['time']; $diskfree = $stats[ 'df'];
Here is a section from the index page on my maintenance site at 10:56am on 29 April using echo " Names : ".sprintf( $format, number_format( $nameNo ), number_format( $nameSize/1024 )).date("d M y H:i", $nameTime ));. The ‘Clear’s are links to macros--not on this page, obviously--which clear that group from the cache.
Files within the Cache directory:
Group Files Size kB Oldest -------------- ------- ------- --------------- Names : 502 7,810 27 Apr 04 07:19 (Clear) EEPROMs : 1,380 3,779 27 Apr 04 07:13 (Clear) Chips : 247 4,442 27 Apr 04 07:27 Chips (search) : 247 814 27 Apr 04 07:27 Chips (total) : 494 5,256 (Clear) Hits : 1 7 29 Apr 04 07:04 (Clear) -------------- ------- ------- --------------- Total : 2,377 16,851 (Clear all) Disk free space : 3,752,472