Traits & interfaces for utilizing cache mechanisms to store frequently retrieved data
MIT License
Traits & interfaces for utilizing cache mechanisms to store frequently retrieved data in Laravel applications.
You can install the package via composer:
composer require sfneal/caching
During the first call to (new CountUnreadInquiriesQuery())->fetch(600)
in a fresh application instance
(or for the first time since flushing the cache) the output of the execute method will be stored in
the Redis cache for 5 minutes (600 seconds).
If another call to (new CountUnreadInquiriesQuery())->fetch(600)
is made within the next 5 minutes, the
previous output will be retrieved from the Redis cache, forgoing the need to execute a full query to the
database. In this example the time saved is minimal but time saving become increasingly significant as
the complexity of a query increases.
# Importing an example model that extends Sfneal/Models/AbstractModels
use App\Models\Inquiry;
# Import Cacheable trait that stores the output of the execute method in a Redis cache using the cacheKey method to set
# the key. Any serializable output from execute can be stored, in this case we're simply storing an integer
use Sfneal\Caching\Traits\Cacheable;
# Importing AbstractQuery as we're building an Eloquent query cache
use Sfneal\Queries\AbstractQuery;
class CountUnreadInquiriesQuery extends AbstractQuery
{
use Cacheable;
/**
* Retrieve the number of unread Inquiries.
*
* @return int
*/
public function execute(): int
{
return Inquiry::query()->whereUnread()->count();
}
/**
* Retrieve the Queries cache key.
*
* @return string
*/
public function cacheKey(): string
{
# using AbstractModel::getTableName() to dynamically retrievethetable name to set the cache prefix
return Inquiry::getTableName().':unread:count';
}
}
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the PHP Package Boilerplate.