Skip to content

Value cache

Often we need to cache some data (usually number) that the application can reuse between requests.

Value cache solves that issue.

Implementation

There is an abstract class that you should extend: \App\Values\CachedValue.

Usually you need to implement a single method actual() that returns the actual value:

php
final class TotalLocalGroups extends CachedValue
{
    /** @inheritDoc */
    protected static function actual(): int
    {
        return LocalGroup::query()->count();
    }
}

And then use the only one available public method: TotalLocalGroups::value().

Optionally, you can also override other methods:

php
\App\Values\CachedValue::cacheFor(...);
\App\Values\CachedValue::cacheKey(...);

All methods are static to simplify usages in Blade templates.