Documentation

CachedHelperTrait

Some handy cache bits.

These is only a runtime cache per object. So it's very short lived.

It's just for when you've done some computation and you don't want store it somewhere without it getting in the way. It just lives within the model as it travels through your code.

Provided you're using the typical serialization provided by SerializeTrait, the internal cache array should not be stored at any point.

Table of Contents

Properties

$_cache  : mixed

Methods

clearCache()  : void
Trash this cache.
getCachedDate()  : DateTimeInterface
Get a date object version of the 'property' field.
getCachedIterable()  : array<string|int, mixed>
Cache an iterable result as an array.
getCachedResult()  : mixed
Cache the result of a function against a hash of the input.
getCachedValue()  : mixed
Cache the result of a function.

Properties

Methods

clearCache()

Trash this cache.

protected clearCache([string|null $key = null ]) : void

Maybe you've mutated something. Go on. Trash it.

Provide the key parameter to selectively delete a cached item.

Recommended placements:

  • __clone()
  • DataObject::update()
Parameters
$key : string|null = null

getCachedDate()

Get a date object version of the 'property' field.

protected getCachedDate(string $property) : DateTimeInterface
Parameters
$property : string
Return values
DateTimeInterface

getCachedIterable()

Cache an iterable result as an array.

protected getCachedIterable(string $id, callable|iterable<string|int, mixed> $fn) : array<string|int, mixed>

More useful than you might think. This takes anything you yield and dumps it in a cached array, and also returns it.

Example:

function getItems(): array
{
    return $this->getCachedIterable('items', function() {
        yield 'key1' => 'one';
        yield 'key2' => 'two';
    });
}
Parameters
$id : string
$fn : callable|iterable<string|int, mixed>

() => iterable

Return values
array<string|int, mixed>

getCachedResult()

Cache the result of a function against a hash of the input.

protected getCachedResult(array<string|int, mixed> $inputs, mixed $fn) : mixed

This is largely identical to getCachedValue() but a bit more flexible.

Your inputs must be serializable.

Parameters
$inputs : array<string|int, mixed>
$fn : mixed

(...$inputs) => mixed

getCachedValue()

Cache the result of a function.

protected getCachedValue(string $id, callable $fn) : mixed

Use this with caution plz.

Parameters
$id : string
$fn : callable

() => mixed

Return values
mixed —

function result

On this page

Search results