Blog

Introducing Pods Alternative Cache

Along with today’s release of Pods 2.4 we are also happy to introduce you to Pods Alternative Cache, or new caching engine for Pods.

Pods Alternative Cache is a file-based or database-based caching solution for hosts that have limitations on object caching. Pods Alternative Cache provides optimal performance with Pods sites on hosts with no object caching or low limits.

It was developed for and tested against the WPEngine platform to improve performance of cached objects generated from Pods. We recommend those using WPEngine to try out this plugin to see if it helps reduce page load across their site.

Why an Alternative Cache?

Hosts like WPEngine have limits set on their object caching engine that are based on what they find optimal for their environment. Sometimes, plugins, themes, and even WordPress core can utilize object cache to the point where it gets too full. When that happens, certain caching engines like APC can remove objects from their cache and that can cause what appears to be random numbers of queries on each page load.

What Pods Alternative Cache does is store all of the Pods objects that need caching, separate from the default object caching engine. Depending on the environment or site, this may still not be optimal. You’ll want to test things out and keep an eye on your site’s performance to see if it’s the right fit for you.

Using the Alternative Cache In Your Own Plugin Or Theme

Not only can Pods use the Alternative Cache, but you can also take advantage of it by using certain caching functions in Pods. Both sets of the Pods cache and transient functions mirror WP Core’s own functions of similar names, but instead use the Alternative Cache. You can also leverage alternative cache by using pods_view for partial page caching.

pods_cache_set

Adds an item to the object cache.

pods_cache_set ( $key, $value, $group = '', $expires = 0)

The first two parameters are required. The first $key is an identifying name for the item to be cached. The second, $value is the item to be cached itself. The third parameter $group, can be used to assign the item to a group. The last parameter $expire is the maximum length of time that the item will be cached for.

Due to the nature of caching, this is a maximum amount of time, not a guaranteed life-span for the cached item. Any data that you need to ensure will always be set, you should save it to a Pods field or use the Options API.

pods_cache_get

Retrieves a cached item from object cache that was added via pods_cache_set.
pods_cache_get ( $key, $group = '', $callback = null )

The first parameter is required. It corresponds to the key assigned to the item in pods_cache_set(). The optional parameter $callback is the name of a callback function that can be used to create the item if it is not already cached.

pods_cache_clear

Clears an item from the object cache or all items from a cache group.

pods_cache_clear ( $key = true, $group = '' )

The first parameter $key can be set to the name of any items added to the object cache using pods_cache_set(). Alternatively, it can be set to true and all items in a group, who’s name it set with the second parameter $group will be cleared.

pods_transient_set

Adds an item to the transient cache using the Transients API.

pods_transient_set ( $key, $value, $expires = 0 )

The first two parameters are required. The first $key is an identifying name for the item to be cached. The second, $value is the item to be cached itself. The last parameter $expire is the maximum length of time that the item will be cached for.

Due to the nature of caching, this is a maximum amount of time, not a guaranteed life-span for the cached item. Any data that you need to ensure will always be set, you should save it to a Pods field or use the Options API.

pods_transient_get

Retrieves a cached item from transient cache that was added via pods_transient_set().

pods_transient_get ( $key, $callback = null )

The first parameter is required. It corresponds to the key assigned to the item in pods_cache_set(). The optional parameter $callback is the name of a callback function that can be used to create the item if it is not already cached.

pods_transient_clear

Clears an item from the transient cache that was added via pods_transient_set().

pods_transient_clear ( $key = true )

The parameter $key can be used to specify a specific item to clear, or it set to true will clear all transients set using pods_transient_set().

Usage

To clear the Pods Alternative Cache, go to Pods Admin > Clear Pods Cache

To disable the Pods Alternative Cache (default: true):

define( 'PODS_ALT_CACHE', false );

To use the database for the storage instead of the filesystem (default: file):

define( 'PODS_ALT_CACHE_TYPE', 'db' );

To use a different filesystem location to store the cache (default: wp-content/podscache):

define( 'PODS_ALT_FILE_CACHE_DIR', 'path/to/podscache' );

Download and Source Code Links