Cache

This component implements callbacks and methods for managing cache using WordPress Transients and Metadata API. By default, the cached values are stored in the database, but you can switch to the in-memory cache with Redis or Memcached. If you need to disable the HivePress cache, set the HP_CACHE constant to false:

define( 'HP_CACHE', false );

Quick example

The function below counts the number of listings added by a specific user. It caches the listing count to prevent the calculation on every call and stores the cache until some listing is added, removed or updated.

function get_listing_count( $user_id ) {

	// Get the cached value.
	$listing_count = hivepress()->cache->get_user_cache( $user_id, 'listing_count', 'models/listing' );

	if ( is_null( $listing_count ) ) {

		// Count listings.
		$listing_count = HivePress\Models\Listing::query()->filter(
			[
				'user'       => $user_id,
				'status__in' => [ 'draft', 'pending', 'publish' ],
			]
		)->get_count();

		// Cache the calculated value.
		hivepress()->cache->set_user_cache( $user_id, 'listing_count', 'models/listing', $listing_count );
	}

	return $listing_count;
}

Storing cache

To store a value in cache, call the set_cache method with the cache key:

hivepress()->cache->set_cache( 'custom_key', null, 'custom_value' );

For grouping the cached values, provide the group name. Both the key and the group name should be as unique as possible to avoid conflicts with other cached values.

hivepress()->cache->set_cache( 'custom_key', 'custom_group', 'custom_value' );

You can also set a time limit for storing the cached value:

hivepress()->cache->set_cache( 'custom_key', 'custom_group', 'custom_value', DAY_IN_SECONDS );

Set the model name as a cache group to clear cache automatically when the model objects are updated. For example, the code below clears the cached value if any listing is added, removed, or updated:

hivepress()->cache->set_cache( 'custom_key', 'models/listing', 'custom_value' );

Similarly, you can store the cache for a specific user, post, comment or term by ID. For example, if you count the number of listings for a user with ID 123, you can store it this way:

hivepress()->cache->set_user_cache( 123, 'listing_count', 'models/listing', $listing_count );

The set_post_cache, set_comment_cache and set_term_cache methods accept the same parameters.

Retrieving cache

To retrieve a cached value, call the get_cache method with the cache key:

$value = hivepress()->cache->get_cache( 'custom_key' );

It returns null if the cache doesn't exist. For retrieving cache related to some group, provide the group name:

$value = hivepress()->cache->get_cache( 'custom_key', 'custom_group' );

Similarly, you can retrieve the cache for a specific user, post, comment or term by ID. For example, HivePress caches the number of listings added by each user, and you can retrieve it this way:

$listing_count = hivepress()->cache->get_user_cache( 123, 'listing_count', 'models/listing' );

The get_post_cache, get_comment_cache and get_term_cache methods accept the same parameters.

Clearing cache

The cache is cleared automatically if the time limit is reached or the related model objects are updated. Also, if the in-memory cache is enabled, it may be cleared if the maximum allowed storage is exceeded.

You can clear the cache explicitly by calling the delete_cache method with the cache key:

hivepress()->cache->delete_cache( 'custom_key' );

To clear the cache related to some group, provide the group name:

hivepress()->cache->delete_cache( 'custom_key', 'custom_group' );

For clearing the whole cache group, skip the cache key parameter:

hivepress()->cache->delete_cache( null, 'custom_group' );

Similarly, you can clear the cache for a specific user, post, comment or term by ID. For example, HivePress caches the number of listings added by each user, and you can clear it this way:

hivepress()->cache->delete_user_cache( 123, 'listing_count', 'models/listing' );

The delete_post_cache, delete_comment_cache and delete_term_cache methods accept the same parameters.

Last updated