122 lines
3.1 KiB
Go
122 lines
3.1 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 using AccessCount from FileInfo.
|
|
func EvictLFU(v vfs.VFS, bytesNeeded uint) uint {
|
|
switch fs := v.(type) {
|
|
case *memory.MemoryFS:
|
|
return fs.EvictLFU(bytesNeeded)
|
|
case *disk.DiskFS:
|
|
return fs.EvictLFU(bytesNeeded)
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// EvictHybrid implements a documented size+recency+frequency hybrid (uses GetTimeDecayedScore; lower=evict first).
|
|
func EvictHybrid(v vfs.VFS, bytesNeeded uint) uint {
|
|
switch fs := v.(type) {
|
|
case *memory.MemoryFS:
|
|
return fs.EvictHybrid(bytesNeeded)
|
|
case *disk.DiskFS:
|
|
return fs.EvictHybrid(bytesNeeded)
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|