The Theory of CachingThere are three key concepts in Zend_Cache. One is the unique identifier (a string) that is used to identify cache records. The second one is the 'lifetime' directive as seen in the examples; it defines for how long the cached resource is considered 'fresh'. The third key concept is conditional execution so that parts of your code can be skipped entirely, boosting performance. The main frontend function (e.g. Zend_Cache_Core::get()) is always designed to return FALSE for a cache miss if that makes sense for the nature of a frontend. That enables end-users to wrap parts of the code they would like to cache (and skip) in if(){ ... } statements where the condition is a Zend_Cache method itself. On the end if these blocks you must save what you've generated, however (e.g. Zend_Cache_Core::save()).
The Zend_Cache Factory MethodA good way to build a usable instance of a Zend_Cache Frontend is given in the following example :
In the following examples we will assume that the $cache variable holds a valid, instantiated frontend as shown and that you understand how to pass parameters to your chosen backends.
Tagging RecordsTags are a way to categorize cache records. When you save a cache with the save() method, you can set an array of tags to apply for this record. Then you will be able to clean all cache records tagged with a given tag (or tags):
Cleaning the CacheTo remove or invalidate in particular cache id, you can use the remove() method :
To remove or invalidate several cache ids in one operation, you can use the clean() method. For example to remove all cache records :
If you want to remove cache entries matching the tags 'tagA' and 'tagC':
If you want to remove cache entries not matching the tags 'tagA' or 'tagC':
If you want to remove cache entries matching the tags 'tagA' or 'tagC':
Available cleaning modes are: CLEANING_MODE_ALL, CLEANING_MODE_OLD, CLEANING_MODE_MATCHING_TAG, CLEANING_MODE_NOT_MATCHING_TAG and CLEANING_MODE_MATCHING_ANY_TAG. The latter are, as their names suggest, combined with an array of tags in cleaning operations.
|