Enhance caching mechanisms and introduce adaptive features

- Updated caching logic to support size-based promotion filtering, ensuring that not all files may be promoted based on size constraints.
- Implemented adaptive caching strategies with a new AdaptiveCacheManager to analyze access patterns and adjust caching strategies dynamically.
- Introduced predictive caching features with a PredictiveCacheManager to prefetch content based on access patterns.
- Added a CacheWarmer to preload popular content into the cache, improving access times for frequently requested files.
- Refactored memory management with a DynamicCacheManager to adjust cache sizes based on system memory usage.
- Enhanced VFS interface and file metadata handling to support new features and improve performance.
- Updated tests to validate new caching behaviors and ensure reliability of the caching system.
This commit is contained in:
2025-09-21 22:47:13 -05:00
parent bbe014e334
commit 45ae234694
12 changed files with 2212 additions and 189 deletions

View File

@@ -3,8 +3,7 @@ package vfs
import (
"io"
"os"
"time"
"s1d3sw1ped/SteamCache2/vfs/types"
)
// VFS defines the interface for virtual file systems
@@ -19,7 +18,7 @@ type VFS interface {
Delete(key string) error
// Stat returns information about the file at the given key
Stat(key string) (*FileInfo, error)
Stat(key string) (*types.FileInfo, error)
// Name returns the name of this VFS
Name() string
@@ -31,82 +30,17 @@ type VFS interface {
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"`
}
// FileInfo is an alias for types.FileInfo for backward compatibility
type FileInfo = types.FileInfo
// 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,
}
}
// NewFileInfo is an alias for types.NewFileInfo for backward compatibility
var NewFileInfo = types.NewFileInfo
// 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,
}
}
// NewFileInfoFromOS is an alias for types.NewFileInfoFromOS for backward compatibility
var NewFileInfoFromOS = types.NewFileInfoFromOS
// UpdateAccess updates the access time and increments the access count
func (fi *FileInfo) UpdateAccess() {
fi.ATime = time.Now()
fi.AccessCount++
}
// BatchedTimeUpdate is an alias for types.BatchedTimeUpdate for backward compatibility
type BatchedTimeUpdate = types.BatchedTimeUpdate
// BatchedTimeUpdate provides a way to batch time updates for better performance
type BatchedTimeUpdate struct {
currentTime time.Time
lastUpdate time.Time
updateInterval time.Duration
}
// NewBatchedTimeUpdate creates a new batched time updater
func NewBatchedTimeUpdate(interval time.Duration) *BatchedTimeUpdate {
now := time.Now()
return &BatchedTimeUpdate{
currentTime: now,
lastUpdate: now,
updateInterval: interval,
}
}
// GetTime returns the current cached time, updating it if necessary
func (btu *BatchedTimeUpdate) GetTime() time.Time {
now := time.Now()
if now.Sub(btu.lastUpdate) >= btu.updateInterval {
btu.currentTime = now
btu.lastUpdate = now
}
return btu.currentTime
}
// UpdateAccessBatched updates the access time using batched time updates
func (fi *FileInfo) UpdateAccessBatched(btu *BatchedTimeUpdate) {
fi.ATime = btu.GetTime()
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
}
// NewBatchedTimeUpdate is an alias for types.NewBatchedTimeUpdate for backward compatibility
var NewBatchedTimeUpdate = types.NewBatchedTimeUpdate