trustwallet-assets/internal/file/cache.go
Daniel d547fb1473
Move CI scripts logic from assets-go-libs (#16680)
* Move CI scripts logic from assets-go-libs

* Add Makefile, .golangci.yml and lint jobs to workflow

* Fix
2021-12-20 00:39:53 +03:00

59 lines
1.0 KiB
Go

package file
import (
"path/filepath"
"strings"
"sync"
)
type Service struct {
mu *sync.RWMutex
cache map[string]*AssetFile
}
func NewService(filePaths ...string) *Service {
var filesMap = make(map[string]*AssetFile)
for _, path := range filePaths {
assetFile := NewAssetFile(path)
filesMap[path] = assetFile
}
return &Service{
mu: &sync.RWMutex{},
cache: filesMap,
}
}
func (f *Service) GetAssetFile(path string) *AssetFile {
f.mu.RLock()
defer f.mu.RUnlock()
return f.getFile(path)
}
func (f *Service) UpdateFile(file *AssetFile, newFileBaseName string) {
f.mu.RLock()
defer f.mu.RUnlock()
oldFileBaseName := filepath.Base(file.Path())
for path := range f.cache {
if strings.Contains(path, oldFileBaseName) {
newPath := strings.ReplaceAll(path, oldFileBaseName, newFileBaseName)
f.cache[path] = NewAssetFile(newPath)
}
}
}
func (f *Service) getFile(path string) *AssetFile {
if file, exists := f.cache[path]; exists {
return file
}
assetF := NewAssetFile(path)
f.cache[path] = assetF
return assetF
}