statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 72 forks source link

Redundant Caching in Asset.php's ->exists() Method Causes Missing Metadata During Asset Import #206

Closed SylvesterDamgaard closed 7 months ago

SylvesterDamgaard commented 10 months ago

I'm encountering caching issues with the ->exists() method in the Asset.php file. I have some code that imports media files from our old CMS, but it seems to be missing metadata.

$container = \Statamic\Facades\AssetContainer::find('images');
$asset = Assets::find($container->handle().'::'.$path);
if (!$asset){
  $asset = $container->makeAsset($path);
  $asset->upload($file);
  $asset->save();
}

I noticed that the generateMeta() method, which is called during the save process, doesn't properly handle the metadata section. The issue seems to be that the file list used for comparison is cached and doesn't get updated or cleared, even when media files are saved or deleted.

The method includes the following code:

$files = Blink::once($this->container()->handle().'::files', function () {
    return $this->container()->files();
});

The data returned from $this->container()->files() / $this->container()->contents() is already cached and gets updated during the save/delete process for assets.

I believe the caching in the Asset.php file is redundant and should be removed.

As a temporary workaround, I can call Blink::forget($container->handle().'::files'); before $asset->save(), and metadata is successfully added as expected.

ryanmitchell commented 7 months ago

Closed by https://github.com/statamic/eloquent-driver/pull/218