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

Caching does not work #17

Closed
francoism90 opened this issue Feb 22, 2024 · 2 comments
Closed

Caching does not work #17

francoism90 opened this issue Feb 22, 2024 · 2 comments

Comments

@francoism90
Copy link
Contributor

I have tried multiple drivers, but it seems to fail at:

if ($this->config->shouldUseCache() && $this->config->cacheDriver->has($this->config->cacheId)) {

This is my ComponentScout:

<?php

use Illuminate\View\Component;
use Spatie\StructureDiscoverer\Cache\FileDiscoverCacheDriver;
use Spatie\StructureDiscoverer\Discover;
use Spatie\StructureDiscoverer\StructureScout;

class ComponentScout extends StructureScout
{
    public ?string $path = null;

    public ?string $prefix = null;

    protected function definition(): Discover
    {
        return Discover::in($this->path)
            ->withCache($this->identifier(), $this->cacheDriver())
            ->parallel()
            ->extending(Component::class)
            ->full();
    }

    public function identifier(): string
    {
        return $this->prefix ?? static::class;
    }

    public function cacheDriver(): FileDiscoverCacheDriver
    {
        return new FileDiscoverCacheDriver(
            directory: sys_get_temp_dir(),
            filename: $this->identifier(),
        );
    }

    public function prefix(string $prefix): static
    {
        $this->prefix = $prefix;

        return $this;
    }

    public function path(string $path): static
    {
        $this->path = $path;

        return $this;
    }
}

This is how I call the scout:

$components = ComponentScout::create()
            ->path('App')
            ->prefix('my-components')
            ->get();

It does find them, but it doesn't use caching at all. It also doesn't create the /tmp/my-components file.

@francoism90
Copy link
Contributor Author

This seem to work:

$scout = ComponentScout::create()
            ->path($path)
            ->prefix($prefix);

        $structure = $scout->isCached()
            ? $scout->get()
            : $scout->cache();

So if I understand correctly, you need to first make the cache (this calls put), and later use get, to retrieve the cached result.

Should the isCached() not be part of the get() method on the Discover class?

Thanks.

@freekmurze
Copy link
Member

We'll handle this further in #18

rubenvanassche added a commit that referenced this issue Mar 13, 2024
Create cache when requested (fixes #17)
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

No branches or pull requests

2 participants