Files
steamcache2/vfs/eviction/eviction.go
Justin Harms bfe29dea75
All checks were successful
Release Tag / release (push) Successful in 9s
Refactor caching and memory management components
- Updated the caching logic to utilize a predictive cache warmer, enhancing content prefetching based on access patterns.
- Replaced the legacy warming system with a more efficient predictive approach, allowing for better performance and resource management.
- Refactored memory management to integrate dynamic cache size adjustments based on system memory usage, improving overall efficiency.
- Simplified the VFS interface and improved concurrency handling with sharded locks for better performance in multi-threaded environments.
- Enhanced tests to validate the new caching and memory management behaviors, ensuring reliability and performance improvements.
2025-09-22 01:59:15 -05:00

111 lines
2.9 KiB
Go

package eviction
import (
"s1d3sw1ped/steamcache2/vfs"
"s1d3sw1ped/steamcache2/vfs/disk"
"s1d3sw1ped/steamcache2/vfs/memory"
)
// EvictionStrategy defines different eviction strategies
type EvictionStrategy string
const (
StrategyLRU EvictionStrategy = "lru"
StrategyLFU EvictionStrategy = "lfu"
StrategyFIFO EvictionStrategy = "fifo"
StrategyLargest EvictionStrategy = "largest"
StrategySmallest EvictionStrategy = "smallest"
StrategyHybrid EvictionStrategy = "hybrid"
)
// EvictLRU performs LRU eviction by removing least recently used files
func EvictLRU(v vfs.VFS, bytesNeeded uint) uint {
switch fs := v.(type) {
case *memory.MemoryFS:
return fs.EvictLRU(bytesNeeded)
case *disk.DiskFS:
return fs.EvictLRU(bytesNeeded)
default:
return 0
}
}
// EvictFIFO performs FIFO (First In First Out) eviction
func EvictFIFO(v vfs.VFS, bytesNeeded uint) uint {
switch fs := v.(type) {
case *memory.MemoryFS:
return fs.EvictFIFO(bytesNeeded)
case *disk.DiskFS:
return fs.EvictFIFO(bytesNeeded)
default:
return 0
}
}
// EvictBySizeAsc evicts smallest files first
func EvictBySizeAsc(v vfs.VFS, bytesNeeded uint) uint {
switch fs := v.(type) {
case *memory.MemoryFS:
return fs.EvictBySize(bytesNeeded, true) // true = ascending (smallest first)
case *disk.DiskFS:
return fs.EvictBySize(bytesNeeded, true) // true = ascending (smallest first)
default:
return 0
}
}
// EvictBySizeDesc evicts largest files first
func EvictBySizeDesc(v vfs.VFS, bytesNeeded uint) uint {
switch fs := v.(type) {
case *memory.MemoryFS:
return fs.EvictBySize(bytesNeeded, false) // false = descending (largest first)
case *disk.DiskFS:
return fs.EvictBySize(bytesNeeded, false) // false = descending (largest first)
default:
return 0
}
}
// EvictLargest evicts largest files first
func EvictLargest(v vfs.VFS, bytesNeeded uint) uint {
return EvictBySizeDesc(v, bytesNeeded)
}
// EvictSmallest evicts smallest files first
func EvictSmallest(v vfs.VFS, bytesNeeded uint) uint {
return EvictBySizeAsc(v, bytesNeeded)
}
// EvictLFU performs LFU (Least Frequently Used) eviction
func EvictLFU(v vfs.VFS, bytesNeeded uint) uint {
// For now, fall back to size-based eviction
// TODO: Implement proper LFU tracking
return EvictBySizeAsc(v, bytesNeeded)
}
// EvictHybrid implements a hybrid eviction strategy
func EvictHybrid(v vfs.VFS, bytesNeeded uint) uint {
// Use LRU as primary strategy, but consider size as tiebreaker
return EvictLRU(v, bytesNeeded)
}
// GetEvictionFunction returns the eviction function for the given strategy
func GetEvictionFunction(strategy EvictionStrategy) func(vfs.VFS, uint) uint {
switch strategy {
case StrategyLRU:
return EvictLRU
case StrategyLFU:
return EvictLFU
case StrategyFIFO:
return EvictFIFO
case StrategyLargest:
return EvictLargest
case StrategySmallest:
return EvictSmallest
case StrategyHybrid:
return EvictHybrid
default:
return EvictLRU
}
}