Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow a Closure to be passed as a ttl in Cache remember() method #39678

Merged
merged 2 commits into from
Nov 18, 2021
Merged

[8.x] Allow a Closure to be passed as a ttl in Cache remember() method #39678

merged 2 commits into from
Nov 18, 2021

Conversation

gerardnll
Copy link
Contributor

@gerardnll gerardnll commented Nov 18, 2021

This PR simplifies the code needed in case the $ttl is calculated each time we have to store a value. I simply added the value() function for the $ttl parameter.

In the following code i want to know how many events the User has hosted so I'm calculating the ttl taking in account when is the next hosted event for that user ending, so the counter will be recalculated because the stored cached value will be expired by then.

function countEventsHostedCached($user) {
    if (Cache::has('count_events_hosted')) {
        return Cache::get('count_events_hosted', 0);
    }

    $closestEventEndsAt = $user->hostedEvents()
        ->select('ends_at')
        ->where('starts_at', '>', Date::now())
        ->orderBy('ends_at')
        ->limit(1)
        ->value('ends_at');

    $value = $user->hostedEvents()->ended()->count();

    Cache::put(
        'count_events_hosted',
        $value,
        // Expire when closest event ends
        $closestEventEndsAt ? Date::parse($closestEventEndsAt) : 60
    );

    return $value;
}

Turns into:

function countEventsHostedCached($user) {
    return Cache::remember(
        'count_events_hosted',
        function () use ($user) {
            // Expire when closest event ends
            $closestEventEndsAt = $user->hostedEvents()
                ->select('ends_at')
                ->where('starts_at', '>', Date::now())
                ->orderBy('ends_at')
                ->limit(1)
                ->value('ends_at');

            return $closestEventEndsAt ? Date::parse($closestEventEndsAt) : 60;
        },
        function () use ($user) {
            return $user->hostedEvents()->ended()->count();
        }
    );
} 

When a Closure is passed to the remember function
@GrahamCampbell GrahamCampbell changed the title Allow a Callable to be passed as a ttl in Cache remember() method [8.x] Allow a Callable to be passed as a ttl in Cache remember() method Nov 18, 2021
@@ -373,7 +373,7 @@ public function forever($key, $value)
* Get an item from the cache, or execute the given Closure and store the result.
*
* @param string $key
* @param \DateTimeInterface|\DateInterval|int|null $ttl
* @param \Closure|\DateTimeInterface|\DateInterval|int|null $ttl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says callable, but this is not a callable. this is more specific, a closure, not all callables are closures. all closures are callable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse my wording. I guess Closure is more appropriate.

@gerardnll gerardnll changed the title [8.x] Allow a Callable to be passed as a ttl in Cache remember() method [8.x] Allow a Closure to be passed as a ttl in Cache remember() method Nov 18, 2021
@taylorotwell taylorotwell merged commit c26f46f into laravel:8.x Nov 18, 2021
@gerardnll gerardnll deleted the patch-1 branch November 18, 2021 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants