> For the complete documentation index, see [llms.txt](https://docs.hivepress.io/developer-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hivepress.io/developer-docs/framework/components/cache.md).

# Cache

This component implements callbacks and methods for managing cache using WordPress [Transients](https://developer.wordpress.org/apis/handbook/transients/) and [Metadata](https://developer.wordpress.org/apis/handbook/metadata/) API. By default, the cached values are stored in the database, but you can switch to the in-memory cache with [Redis](https://redis.io/) or [Memcached](https://memcached.org/). If you need to disable the HivePress cache, set the `HP_CACHE` constant to `false`:

```php
define( 'HP_CACHE', false );
```

### Quick example <a href="#bkmrk-quick-example" id="bkmrk-quick-example"></a>

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.

```php
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 <a href="#bkmrk-storing-cache" id="bkmrk-storing-cache"></a>

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

```php
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.

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

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

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

Set the [model](/developer-docs/framework/models.md) 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:

```php
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:

```php
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 <a href="#bkmrk-retrieving-cache" id="bkmrk-retrieving-cache"></a>

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

```php
$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:

```php
$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:

```php
$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 <a href="#bkmrk-clearing-cache" id="bkmrk-clearing-cache"></a>

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:

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

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

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

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

```php
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:

```php
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.
