Cache
The framework comes integrated with a Cache system, it is as a modified version of phpFastCache.
The cache system supports the following storage types: Redis, Predis, Cookie, Files, MemCache, MemCached, APC, WinCache and X-Cache.
Get
Before start using Cache, add the use
statement at the beginning of your PHP file to call it.
use WPMVC\Cache;
To get a cached value use the static method "get" and retrieve the value using a "key".
$value = Cache::get( 'cache_key' );
Exists
To check if there is a value cached use the static method "has".
if ( Cache::has('cache_key') ) { $value = Cache::get('cache_key'); }
Add
Adding a value will require an expiration time in minutes. This value will determine when the cache should clear the value from storage.
$value = 'Example string.'; $expiresAtMinute = 10; // Expiration Cache::add( 'cache_key', $value, $expiresAtMinute );
There is an alternate, yet more flexible, way of setting (adding) and getting a value, and it is by using the method remember()
; it receives as the second parameter which is the interval in which the value should be refreshed and the callable
to refresh to.
$value = Cache::remember( 'cache_key', $refreshAtMinute = 10, function () { return 'Test string value'; } );
Remove
To remove a value use the static method "forget".
Cache::forget('cache_key');
To empty the entire cache use the static method "flush".
Cache::flush();
Configuration
The cache can be configured in the configuration file. Changing the engine, storage location and even disabling the whole system is possible, although it is recommended to read phpFastCache documentation first.