Dev Blog
When dealing with caching, common task is to load
cache value, check if it
is not empty and depending on it - either set some variable or
perform some operation to retrieve this value.
This common tasks might scattered around various places in
application.
New cache component feature comes as
remedy, allowing to execute callback if value is not already
cached. This makes possible to use one-liners to either get
cached value or retrieve it from any source.
Example use case
Assume that we need to load some data from remote database, which
might take long. So we wan't to cache it for one minute. With
additional callback parameter passed to
get
method.
As a third parameter, timeout might be passed.
In example below, data will be loaded no more than once per
minute. Without any additional if statements.
<?php use Maslosoft\Cache\Cache; class MyComponent { public function __construct() { $data = Cache::fly()->get('data-key', [$this, 'loadData'], Cache::Minute); } public function loadData() { return MyModel::loadData(); } }