github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/project/cache.go (about)

     1  package project
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/tooploox/oya/pkg/oyafile"
     7  	"github.com/tooploox/oya/pkg/raw"
     8  )
     9  
    10  func (p *Project) oyafileIn(dir string) (*oyafile.Oyafile, bool, error) {
    11  	normalizedDir := filepath.Clean(dir)
    12  	o, found := p.oyafileCache[normalizedDir]
    13  	if found {
    14  		return o, true, nil
    15  	}
    16  	o, found, err := oyafile.LoadFromDir(dir, p.RootDir)
    17  	if err != nil {
    18  		return nil, false, err
    19  	}
    20  	if found {
    21  		p.oyafileCache[normalizedDir] = o
    22  		return o, true, nil
    23  	}
    24  
    25  	return nil, false, nil
    26  }
    27  
    28  func (p *Project) rawOyafileIn(dir string) (*raw.Oyafile, bool, error) {
    29  	// IMPORTANT: Call invalidateOyafileCache after patching raw Oyafiles
    30  	// obtained using this method!
    31  	normalizedDir := filepath.Clean(dir)
    32  	o, found := p.rawOyafileCache[normalizedDir]
    33  	if found {
    34  		return o, true, nil
    35  	}
    36  	o, found, err := raw.LoadFromDir(dir, p.RootDir)
    37  	if err != nil {
    38  		return nil, false, err
    39  	}
    40  	if found {
    41  		p.rawOyafileCache[normalizedDir] = o
    42  		return o, true, nil
    43  	}
    44  	return nil, false, nil
    45  }
    46  
    47  func (p *Project) invalidateOyafileCache(dir string) {
    48  	delete(p.oyafileCache, dir)
    49  	delete(p.rawOyafileCache, dir)
    50  }
    51  
    52  func (p *Project) Oyafile(oyafilePath string) (*oyafile.Oyafile, bool, error) {
    53  	// BUG(bilus): Uncached (but used only by Render).
    54  	return oyafile.Load(oyafilePath, p.RootDir)
    55  }