Cache
Metapress provides two cache classes that can be used to store and retrieve data from the WordPress cache. The ObjectCache class is used to store data in the WordPress cache, while the Transient class is used to store data in the WordPress transient cache.
These classes can be found under the Cache namespace in the Metapress framework.
Object Cache
The ObjectCache class is used to store data in the WordPress cache. It provides two methods for storing and retrieving data:
remember(): Stores data in the cache.forget(): Removes data from the cache.
NOTE
All methods are static methods and can be called directly without creating an instance of the class.
Store data
To store data in the cache, you can use the remember() method. This method takes four arguments: the key, callback, the group, and the expiration time.
The key is a unique identifier for the data, the callback is a function that returns the data, the group is a string that specifies the group of the data, and the expiration time is an integer that specifies the number of seconds until the data expires.
Here's an example of how to use the remember() method:
use Sematico\Metapress\Cache\ObjectCache;
function do_something() {
return ObjectCache::remember( 'my_key', function() {
return long_running_function();
} );
}Forget data
To remove data from the cache, you can use the forget() method. This method takes three arguments: the key, and two optional arguments: the group and a default value.
The default value is used if the data is not found in the cache. Defaults to null.
Here's an example of how to use the forget() method:
use Sematico\Metapress\Cache\ObjectCache;
function do_something() {
ObjectCache::forget( 'my_key' );
// Do something else
}Transient
The Transient class is used to store data in WordPress via transients. The following methods are available:
remember(): Retrieve a value from transients. If it doesn't exist, run the $callback to generate and cache the value.forget(): Remove a value from transients.get(): Retrieve a value from transients.
NOTE
All methods are static methods and can be called directly without creating an instance of the class.
Retrieve a value
To retrieve a value from transients, you can use the remember() method. This method takes three arguments: the key, the callback, and the expiration time.
The key is a unique identifier for the value, the callback is a function that returns the value, and the expiration time is an integer that specifies the number of seconds until the value expires.
use Sematico\Metapress\Cache\Transient;
function do_something() {
return Transient::remember( 'my_key', function() {
return long_running_function();
} );
}Forget a value
To remove a value from transients, you can use the forget() method. This method takes two arguments: the key and the default value.
The default value is used if the value is not found in transients. Defaults to null.
use Sematico\Metapress\Cache\Transient;
function do_something() {
Transient::forget( 'my_key' );
// Do something else
}Get a value
To retrieve a value from transients, you can use the get() method. This method takes only one argument: the key.
use Sematico\Metapress\Cache\Transient;
function do_something() {
$value = Transient::get( 'my_key' );
// Do something with the value
}