Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created September 21, 2019 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/9379d52cc51ee1e1b6771af1394fb423 to your computer and use it in GitHub Desktop.
Save IISResetMe/9379d52cc51ee1e1b6771af1394fb423 to your computer and use it in GitHub Desktop.
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