117 lines
2.5 KiB
Go
117 lines
2.5 KiB
Go
package diskcache
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha1" //nolint:gosec
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
// Cache interface for caching operations
|
|
type Cache interface {
|
|
Get(key string) ([]byte, error)
|
|
Set(key string, value []byte) error
|
|
}
|
|
|
|
// FileCache struct for file-based caching
|
|
type FileCache struct {
|
|
dir string
|
|
// granular locks
|
|
scopedLocks struct {
|
|
sync.Mutex
|
|
sync.Once
|
|
locks map[string]sync.Locker
|
|
}
|
|
}
|
|
|
|
// NewFileCache creates a new FileCache
|
|
func NewFileCache(dir string) (*FileCache, error) {
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
return nil, fmt.Errorf("can't make directory %s: %v", dir, err)
|
|
}
|
|
return &FileCache{dir: dir}, nil
|
|
}
|
|
|
|
func (f *FileCache) Store(ctx context.Context, key string, value []byte) error {
|
|
mu := f.getScopedLocks(key)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
fileName := f.getFileName(key)
|
|
if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.WriteFile(fileName, value, 0600); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *FileCache) Load(ctx context.Context, key string) (value []byte, exist bool, err error) {
|
|
r, ok, err := f.open(key)
|
|
if err != nil || !ok {
|
|
return nil, ok, err
|
|
}
|
|
defer r.Close()
|
|
|
|
value, err = io.ReadAll(r)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return value, true, nil
|
|
}
|
|
|
|
func (f *FileCache) Delete(ctx context.Context, key string) error {
|
|
mu := f.getScopedLocks(key)
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
fileName := f.getFileName(key)
|
|
if err := os.Remove(fileName); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *FileCache) open(key string) (*os.File, bool, error) {
|
|
fileName := f.getFileName(key)
|
|
file, err := os.Open(fileName)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil, false, nil
|
|
}
|
|
return nil, false, err
|
|
}
|
|
|
|
return file, true, nil
|
|
}
|
|
|
|
// getScopedLocks pull lock from the map if found or create a new one
|
|
func (f *FileCache) getScopedLocks(key string) (lock sync.Locker) {
|
|
f.scopedLocks.Do(func() { f.scopedLocks.locks = map[string]sync.Locker{} })
|
|
|
|
f.scopedLocks.Lock()
|
|
lock, ok := f.scopedLocks.locks[key]
|
|
if !ok {
|
|
lock = &sync.Mutex{}
|
|
f.scopedLocks.locks[key] = lock
|
|
}
|
|
f.scopedLocks.Unlock()
|
|
|
|
return lock
|
|
}
|
|
|
|
func (f *FileCache) getFileName(key string) string {
|
|
hasher := sha1.New() //nolint:gosec
|
|
_, _ = hasher.Write([]byte(key))
|
|
hash := hex.EncodeToString(hasher.Sum(nil))
|
|
return filepath.Join(f.dir, fmt.Sprintf("%s/%s/%s", hash[:1], hash[1:3], hash))
|
|
}
|