- Introduced a YAML-based configuration system, allowing for automatic generation of a default `config.yaml` file. - Updated the application to load configuration settings from the YAML file, improving flexibility and ease of use. - Added a Makefile to streamline development tasks, including running the application, testing, and managing dependencies. - Enhanced `.gitignore` to include build artifacts and configuration files. - Removed unused Prometheus metrics and related code to simplify the codebase. - Updated dependencies in `go.mod` and `go.sum` for improved functionality and performance.
80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
// vfs/vfs.go
|
|
package vfs
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// VFS defines the interface for virtual file systems
|
|
type VFS interface {
|
|
// Create creates a new file at the given key
|
|
Create(key string, size int64) (io.WriteCloser, error)
|
|
|
|
// Open opens the file at the given key for reading
|
|
Open(key string) (io.ReadCloser, error)
|
|
|
|
// Delete removes the file at the given key
|
|
Delete(key string) error
|
|
|
|
// Stat returns information about the file at the given key
|
|
Stat(key string) (*FileInfo, error)
|
|
|
|
// Name returns the name of this VFS
|
|
Name() string
|
|
|
|
// Size returns the current size of the VFS
|
|
Size() int64
|
|
|
|
// Capacity returns the maximum capacity of the VFS
|
|
Capacity() int64
|
|
}
|
|
|
|
// FileInfo contains metadata about a cached file
|
|
type FileInfo struct {
|
|
Key string `json:"key"`
|
|
Size int64 `json:"size"`
|
|
ATime time.Time `json:"atime"` // Last access time
|
|
CTime time.Time `json:"ctime"` // Creation time
|
|
AccessCount int `json:"access_count"`
|
|
}
|
|
|
|
// NewFileInfo creates a new FileInfo with the given key and current timestamp
|
|
func NewFileInfo(key string, size int64) *FileInfo {
|
|
now := time.Now()
|
|
return &FileInfo{
|
|
Key: key,
|
|
Size: size,
|
|
ATime: now,
|
|
CTime: now,
|
|
AccessCount: 1,
|
|
}
|
|
}
|
|
|
|
// NewFileInfoFromOS creates a FileInfo from os.FileInfo
|
|
func NewFileInfoFromOS(info os.FileInfo, key string) *FileInfo {
|
|
return &FileInfo{
|
|
Key: key,
|
|
Size: info.Size(),
|
|
ATime: time.Now(), // We don't have access time from os.FileInfo
|
|
CTime: info.ModTime(),
|
|
AccessCount: 1,
|
|
}
|
|
}
|
|
|
|
// UpdateAccess updates the access time and increments the access count
|
|
func (fi *FileInfo) UpdateAccess() {
|
|
fi.ATime = time.Now()
|
|
fi.AccessCount++
|
|
}
|
|
|
|
// GetTimeDecayedScore calculates a score based on access time and frequency
|
|
// More recent and frequent accesses get higher scores
|
|
func (fi *FileInfo) GetTimeDecayedScore() float64 {
|
|
timeSinceAccess := time.Since(fi.ATime).Hours()
|
|
decayFactor := 1.0 / (1.0 + timeSinceAccess/24.0) // Decay over days
|
|
frequencyBonus := float64(fi.AccessCount) * 0.1
|
|
return decayFactor + frequencyBonus
|
|
}
|