filebrowser/backend/diskcache/file_cache.go

117 lines
2.5 KiB
Go
Raw Normal View History

2020-07-27 17:01:02 +00:00
package diskcache
import (
"context"
"crypto/sha1" //nolint:gosec
"encoding/hex"
"errors"
"fmt"
2021-12-20 22:16:35 +00:00
"io"
2020-07-27 17:01:02 +00:00
"os"
"path/filepath"
"sync"
)
2024-08-24 22:02:33 +00:00
// Cache interface for caching operations
type Cache interface {
Get(key string) ([]byte, error)
Set(key string, value []byte) error
}
2020-07-27 17:01:02 +00:00
2024-08-24 22:02:33 +00:00
// FileCache struct for file-based caching
type FileCache struct {
dir string
2020-07-27 17:01:02 +00:00
// granular locks
scopedLocks struct {
sync.Mutex
sync.Once
locks map[string]sync.Locker
}
}
2024-08-24 22:02:33 +00:00
// 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)
2020-07-27 17:01:02 +00:00
}
2024-08-24 22:02:33 +00:00
return &FileCache{dir: dir}, nil
2020-07-27 17:01:02 +00:00
}
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)
2024-08-24 22:02:33 +00:00
if err := os.MkdirAll(filepath.Dir(fileName), 0700); err != nil {
2020-07-27 17:01:02 +00:00
return err
}
2024-08-24 22:02:33 +00:00
if err := os.WriteFile(fileName, value, 0600); err != nil {
2020-07-27 17:01:02 +00:00
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()
2021-12-20 22:16:35 +00:00
value, err = io.ReadAll(r)
2020-07-27 17:01:02 +00:00
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)
2024-08-24 22:02:33 +00:00
if err := os.Remove(fileName); err != nil && !errors.Is(err, os.ErrNotExist) {
2020-07-27 17:01:02 +00:00
return err
}
return nil
}
2024-08-24 22:02:33 +00:00
func (f *FileCache) open(key string) (*os.File, bool, error) {
2020-07-27 17:01:02 +00:00
fileName := f.getFileName(key)
2024-08-24 22:02:33 +00:00
file, err := os.Open(fileName)
2020-07-27 17:01:02 +00:00
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))
2024-08-24 22:02:33 +00:00
return filepath.Join(f.dir, fmt.Sprintf("%s/%s/%s", hash[:1], hash[1:3], hash))
2020-07-27 17:01:02 +00:00
}