Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import-Module PSCache
# Define fetcher
$Fetcher = {
# Emulate a potentially slow operation
Start-Sleep -Milliseconds (Get-Random -Minimum 15 -Maximum 30)
# Return an object corresponding to $_
[pscustomobject]@{
ID = $_
Data = "SomeValue${_}"
}
}
# Create PSCache instance with LRU policy
$LRUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy LRU -Capacity 100
# Create PSCache instance with LFU policy
$LFUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy LFU -Capacity 100
# Create PSCache instance with Segmented LRU policy
$SLRUcache = New-PSCache -Fetcher $Fetcher -EvictionPolicy SLRU -Capacity 50
# Prepare our access pattern
$IDs = 1..1000 |ForEach-Object {
1..200 |Get-Random
}
# Without Caching
Write-Host "Testing semi-random access pattern:" -ForegroundColor Green
Measure-Command {
$IDs |ForEach-Object -Process $Fetcher
} |Select-Object @{N='Strategy';E={'No Cache'}},TotalSeconds
# With LRU
Measure-Command {
$IDs |ForEach-Object {
$LRUcache.Get($_)
}
} |Select-Object @{N='Strategy';E={'Evict Least-Recently Used'}},TotalSeconds
# With LFU
Measure-Command {
$IDs |ForEach-Object {
$LFUcache.Get($_)
}
} |Select-Object @{N='Strategy';E={'Evict Least-Frequently Used'}},TotalSeconds
# With SLRU
Measure-Command {
$IDs |ForEach-Object {
$SLRUcache.Get($_)
}
} |Select-Object @{N='Strategy';E={'Evict Segmented LRU'}},TotalSeconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.