Home Blog Simple Cache With PHP

# Simple Cache With PHP

0

It was on May 4th, 2011 that we published an article on Internoetics titled “Cache Your Website with PHP (in 1 minute)”. Now redirecting to this post, the former article was aimed at caching an entire webpage with a function not that different than what is described here. The problem, however, is that if you’re going to cache an entire site, there’s far better ways of doing so than what we had previously described. While we’ll republish the (updated) original code soon, the purpose of this article supplies functions designed to cache small amounts of dynamic data – not an entire page.

In the course of publishing WordPress shortcode in the past, we almost always utilized the framework’s transient features… but naked PHP code generally went without caching – despite desperately needing it. For that reason, as we migrate code over from a plethora of our other websites, we’ll reference a very basic PHP caching function that isn’t entirely dissimilar to WP’s transient API.

### About Simple Cache (and WP’s Transient API)

The principle of the WordPress transient API is simple: it offers a simple and standardized way of storing cached data in a database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted. Our PHP functions emulate this behaviour for use outside of WP (used mainly for our PHP code snippets that require a cache feature).

Why cache data? Caching transient or temporary data is useful when querying rate-limited third-party APIs that provide living and evolving information (such as RSS and news feeds, social counts, your last tweet, currency conversions, interest rates, weather forecasts etc). The idea is that you request data, store it for a particular time period, and then query new data when your defined cache interval has expired. The time period you’ll cache data is usually determined by the API itself, the frequency at which data is updated, or perhaps the time it takes to process new information.

The primary difference between our functions compared to that of WordPress is that we’ll cache data in a text file – not a database. Keep in mind that our solution is intended to be used outside of WordPress… so if you’re using WP, continue to use the transient API.

Our functions will store cached data in text files.

The PHP functions below are designed to emulate WP’s get_transient() and set_transient() functions. They’ll be referenced over and over for future PHP functions we share on our blog. They’re also extremely simple so you can expect them to evolve in a serious way over time.

### beliefmedia_set_transient

Not unlike WordPress’ set_transient function, our simple beliefmedia_set_transient function works in a similar manner. It takes the unique transient name and the data you wish to cache and writes it to a text file in a defined directory. Like WordPress, we’ll serialize the data if it’s an array.

Note that you’ll have to define a location for your cache directory.

### beliefmedia_get_transient

The beliefmedia_get_transient function is intended to emulate the WordPress get_transient function (with the exception that we’ll also provide the cache value as an argument in addition to the transient name). The function simply checks the creation time of the cached file against the current time and cache value. If it’s valid we’ll return the data; if not, we’ll return the boolean false. Not unlike WordPress, we’ll unserialize the data if required (in fact, we use a WordPress function to check).

Note that you’ll have to define a location for your cache directory.

### beliefmedia_is_serialized

To determine if the cached data is serialized before it’s returned, we use WordPress’ is_serialized() function (we’ve prefixed it with beliefmedia to avoid any clashes, but it’s otherwise unaltered). The function will return false if the data isn’t serialized, otherwise it’ll return true.

### Caching Options

We expect that we’ll publish WordPress shortcodes that utilize the Options API . It’s likely we’ll make a similar set of PHP functions available that emulate the same features when it becomes necessary.

An advantage of storing data in text files means that if the data is expired, and a request for new information fails, you can simply request the old information and note its (older) creation time (essentially providing an option without the expiration feature). Something like this will simply return existing data (or return false if it doesn’t exist).

If you chose to include the data’s creation time (handy if you’re returning data which is quite old), simply apply the following:

### Considerations

• If you were to store information in a database rather than text files, you could quite easily alter the function so it more closely resembled WordPress’ transient API.
• It should be noted (again) that the primary purpose of our functions were to make very basic cache functions available that can be referenced when sharing our PHP code snippets.
• Note that we define $location = '/path/to/public_html/your/cache/' .$transient . '.txt'; in each function. If you’re using a configuration file or similar, define this path here and alter the code as necessary.

Update, 1st June 2017: We’ve created a WordPress plugin. Simply upload and activate. A few of our plugins now require it be installed.