Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Failing to Cache a Query

I am using this query wrapped in a cache block:

$domainProject = Cache::tags("customdomains.{$domain}")->remember("customdomains.{$domain}", now()->addHours(12), function () use ($domain) {
    return Domain::select('domains.id', 'domains.name')
    // where has 3 arguments: column, operator, argument to evaluate against
    ->where('domains.name', $domain)
    // join to projects table domains project id with projects table id
    ->join('projects', 'domains.project_id', '=', 'projects.id')
    // We order by ID and descending and then we use the first() method helper.
    // The first method returns the first element in the collection that passes a given truth test
    ->orderBy('id', 'DESC')->first();
});

This rule is however completely ignored. The query is fired every time. Here is the backtrace:

    15. /app/Http/Controllers/Published/SiteController.php:76 //  ->orderBy('id', 'DESC')->first();
    16. /vendor/laravel/framework/src/Illuminate/Cache/Repository.php:385
    17. /app/Http/Controllers/Published/SiteController.php:77 // });
    18. /vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
    19. /vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45

Why is the caching no happening?

0 likes
11 replies
MichalOravec's avatar

You use join so you have to choose from which table you want to order by id

->orderByDesc('domains.id')->first();
1 like
rhand's avatar
Level 6

Hmm adding domains to load the id from the domains table does not change anything yet. The query is still executed and the same backtrace is shown.

And I do see a key added which expires in 12 hrs but with only N; in it

GET "laravel_database_laravel_cache:03643a6ec1565ce435853e068d218fe10570b5c1:customdomains.bikes.smtv.test"
N;
MichalOravec's avatar

Cache tags are not supported when using the file, dynamodb, or database cache drivers.

https://laravel.com/docs/8.x/cache#cache-tags

$domainProject = Cache::remember("customdomains.{$domain}", now()->addHours(12), function () use ($domain) {
    return Domain::select('domains.id', 'domains.name')
        ->join('projects', 'domains.project_id', '=', 'projects.id')
        ->where('domains.name', $domain)
        ->orderByDec('domains.id')
        ->first();
});
1 like
rhand's avatar
Level 6

True, but I am using Redis and for Redis it does work and it does work for other cache tags. Even for one you helped out with the other day. See value N; I got back GETting the key in the Redis Red GUI in previous answer and once more here

GET "laravel_database_laravel_cache:03643a6ec1565ce435853e068d218fe10570b5c1:customdomains.bikes.smtv.test"
N;

Will try without tagging once just to see anyways.. But I think that failed too before.

rhand's avatar
Level 6

When not using tagging, though I think I can with Redis, and using your code I:

$domainProject = Cache::remember("customdomains.{$domain}", now()->addHours(12), function () use ($domain) {
    return Domain::select('domains.id', 'domains.name')
    // where has 3 arguments: column, operator, argument to evaluate against
    ->where('domains.name', $domain)
    // join to projects table domains project id with projects table id
    ->join('projects', 'domains.project_id', '=', 'projects.id')
    // We order by ID and descending and then we use the first() method helper.
    // The first method returns the first element in the collection that passes a given truth test
    ->orderBy('domains.id', 'DESC')->first();
});

got this backtrace and still no luck:

15. /app/Http/Controllers/Published/SiteController.php:77
16. /vendor/laravel/framework/src/Illuminate/Cache/Repository.php:385
17. /vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php:406
18. /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
19. /app/Http/Controllers/Published/SiteController.php:78
rhand's avatar
Level 6

Well, when I load the frontend I expect the query I cache to be skipped on second load. That does not happen.

Two, when I check the value of the added key (does get added into Redis cache) it shows N; as value

2021-05-04 15:14:51 GET "laravel_database_laravel_cache:8f6949e78aa16d33489abb5c6be5d1a5b2e2dd1b:customdomains.213.bikes.smtv.test"
N;

So I guess it reads that and is like Nothing to see here. and moves on.

So I guess the main issue is that the value of the key seems to be wrong and cannot be used to load data and bypass the query.

Snapey's avatar

why do you join to projects table when you don't use it?

1 like
Snapey's avatar

i might be wrong but does the remember method expect a number of seconds - not an end time?

i'm a little cautious because I know this changed at some point

rhand's avatar
Level 6

Well, decided to drop caching this query for now. Do think the join is making caching not possible. As for code that follows. Well there are two more conditionals that follow the code.. but that should not matter...

Please or to participate in this conversation.