v0.8.1 WIP
This commit is contained in:
@ -16,6 +16,7 @@ type CacheData struct {
|
||||
Hash string `yaml:"hash"`
|
||||
Path string `yaml:"path"`
|
||||
Type string `yaml:"type"`
|
||||
URL string `yaml:"url"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
@ -101,13 +102,17 @@ func (c *Cache) AddDataToStore(hash string, cacheData CacheData) error {
|
||||
return c.saveToFile()
|
||||
}
|
||||
|
||||
// Set stores data on disk and in the cache file and returns the cache data
|
||||
// The filepath of the data is the file name + a SHA256 hash of the URL
|
||||
func (c *Cache) Set(source, hash string, data []byte, dataType string) (CacheData, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
sourceHash := HashURL(source)
|
||||
|
||||
fileName := filepath.Base(source)
|
||||
|
||||
path := filepath.Join(c.dir, fmt.Sprintf("%s-%s", fileName, hash))
|
||||
path := filepath.Join(c.dir, fmt.Sprintf("%s-%s", fileName, sourceHash))
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
os.MkdirAll(c.dir, 0700)
|
||||
@ -122,9 +127,10 @@ func (c *Cache) Set(source, hash string, data []byte, dataType string) (CacheDat
|
||||
Hash: hash,
|
||||
Path: path,
|
||||
Type: dataType,
|
||||
URL: sourceHash,
|
||||
}
|
||||
|
||||
c.store[hash] = cacheData
|
||||
c.store[sourceHash] = cacheData
|
||||
|
||||
// Unlock before calling saveToFile to avoid double-locking
|
||||
c.mu.Unlock()
|
||||
@ -184,3 +190,8 @@ func LoadMetadataFromFile(filePath string) ([]*CacheData, error) {
|
||||
|
||||
return cacheData, nil
|
||||
}
|
||||
|
||||
func HashURL(url string) string {
|
||||
hash := sha256.Sum256([]byte(url))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
@ -57,11 +57,12 @@ func NewRemoteFetcher(source string, cache *Cache, options ...FetcherOption) (Re
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hash := fetcher.Hash(data)
|
||||
if cachedData, cacheMeta, exists := cache.Get(hash); exists {
|
||||
URLHash := HashURL(source)
|
||||
if cachedData, cacheMeta, exists := cache.Get(URLHash); exists {
|
||||
return &CachedFetcher{data: cachedData, path: cacheMeta.Path, dataType: cacheMeta.Type}, nil
|
||||
}
|
||||
|
||||
hash := fetcher.Hash(data)
|
||||
cacheData, err := cache.Set(source, hash, data, config.FileType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user