Improved Caching with PHP

Introduction

In the last Code Gallery Spotlight, we examined a PHP class that performed very simple data caching. This time around we are going to explore the same topic, but we will add new functionality in order to increase efficiency and reduce the chance of errors. The class we will be examining this time around uses file locking and employs some better coding techniques than the last class we used.
If you missed the last , you can find it at: https://techsoftcenter.com/caching-variables-with-php/

Improvements

As I mentioned a moment ago, the class we will be examining today has several improvements over the class we looked at last time. For starters, rather than tracking the creation time by appending it to the file name, our new class uses the filemtime function to determine when the cache was last updated. This allows us to use a static file name rather than a dynamic one.

By removing the creation time from the file name, we are also able to achieve another improvement. In the last class we examined, we needed to loop through the filenames in the directory containing the cache files in order to find the proper file. Now that our file name is no longer dynamic, we can eliminate the looping and access the file directly. The more files there are in a particular directory, the more efficient this method will be for that directory.

File Locking

The biggest improvement this new class provides is file locking. File locking eliminates the possibility that data may be read from a cache file while it is being updated. Using file locks also prevents a situation where two processes try and update the cache at the same time.
File locking disables access to a file while some action is being performed on that file. By using file locking you can greatly reduce the chance of file corruption and lost data. In single user applications file locking is not a great concern. When multiple users may be accessing the same files, however, file locking is a must.                                               File locks can be either “mandatory” or “advisory”. When mandatory locks are used, reading and writing to files is not possible while a file is locked. Mandatory locks are usually implemented at the system level. Advisory locks, on the other hand, do allow for these operations to take place during a file lock. For advisory locks to work properly, any read or write operations must check for the existence of a file lock. Advisory locks are also known as cooperative locks.
The file locking method used by this class is an advisory lock and very straightforward. Before an update to the cache file occurs, a copy of the file is made with a .lock extension. The updates are then made to the original cache file without that extension.
During the update process, any requests for the data in the cache file are retrieved from the file with the .lock extension. If another process attempts to update the cache during this time, the existence of the .lock file does not permit the update. After the update is completed, the file with the extension is removed.

Using this method ensures that the cache file is never updated by two processes simultaneously. If that were to happen, there could be very unpredictable data in the cache file.

New Caching Class

This class is a wonderful example of how classes in PHP should be written. Member functions are designed to carry out very specific tasks, the code is extremely well documented, and great care has been taken to ensure the integrity of the data.
Even if you have no need for caching in your applications, I recommend studying this implementation for its elegance in coding.

Code

Example Use

Conclusion

In that article I detailed why and when you should use caching.