thoughtco / statamic-cache-tracker

12 stars 2 forks source link

Cache not cleared on entry create when using entry query #3

Closed stuartcusackie closed 6 months ago

stuartcusackie commented 7 months ago

I'm not sure if this package is intended to cover this particular situation, but let me explain anyway...

I have an events-index.blade.php template for an entry in my 'pages' collection. In this template I find all entries in my 'events' collection using the Entry facade, e.g.

$events = Entry::query()
            ->where('collection', 'events')
            ->where('published', true)
            ->get();

These events are then output in my template like so (blade components):

@foreach($events as $event)
  <x-event-listing :event="$event" />
@endforeach

I was hoping that this package would clear the static cache for this 'pages' entry when I create a new entry in the 'events' collection, but it doesn't seem to.

Is this intended behaviour or do I need to use addAdditionalTracker(), or should I just stick to the rules in config/static_caching.php for this kind of stuff?

Thanks!

ryanmitchell commented 7 months ago

Are you including that using a partial tag? If so you could clear it using a listener:

Tracker::invalidate('partial:events-index');
stuartcusackie commented 7 months ago

I'm not using Antlers and very few tags. My project is all Blade. Here's a simplified version:

@php
$events = Entry::query()
            ->where('collection', 'events')
            ->where('published', true)
            ->get();
@endphp

<x-layout>
  @foreach($events as $event)
    <x-event-listing :event="$event" />
  @endforeach
</x-layout>

Sounds like this package relies on Antlers? Or maybe using the Blade collection tag would solve it? i.e.

@foreach(Statamic::tag('collection:events')->limit(3) as $event)

ryanmitchell commented 7 months ago

It relies on augmentation, not necessarily Antlers. If your entries are being augmented inside your blade component then they should be being picked up and tracked.

You can dump the contents of the cache store to see whats been tracked if you want:

dd(\Thoughtco\StatamicCacheTracker\Facades\Tracker::cacheStore()->get('tracker::urls'));
stuartcusackie commented 7 months ago

After more testing I could see it working, though I had a couple of issues - maybe my fault. Anyway...

The cache IS being cleared when I updating one of the existing event entries. The cache is not being cleared when I create a new event entry, should it be? So it's working on update, but not on create.

Maybe I'm asking too much. The package would have to wipe the caches for any urls that contain 'events' when I create a new event. Is that something you intended? Perhaps it could be an option?

I think a combination of the default static_caching rules and this package should work great for my needs.

Thanks again. You can close this issue if I have this right.

ryanmitchell commented 7 months ago

Creating is where you need to write you own logic, thats why we track what partials are being used, but as you're using blade maybe those dont get tracked (never tried it).

You could maybe hook into view composers to do something similar to: https://github.com/thoughtco/statamic-cache-tracker/blob/e25e1808745fc46c57ca6795ec08122c4cad1957/src/Http/Middleware/CacheTracker.php#L143-L148

And then in an EntryCreated listener (for that collection), clear the right partials?

stuartcusackie commented 6 months ago

I see I see. I'll try that in future if I need something more sophisticated. For the moment I think the default static caching rules along with your package will handle my current needs. Thank you.