我正在研究如何使用 PowerShell 构建缓存过期处理程序。下面的脚本表明了该目标和预期输出。
$RemovedAction = {
param([System.Runtime.Caching.CacheEntryRemovedArguments]$Arguments)
$Key = $Arguments.CacheItem.Key
$Item = $Arguments.CacheItem
$RemovedObject = $Arguments.CacheItem.Value
$RemovedReason = $Arguments.RemovedReason
Write-Host -ForegroundColor Yellow "About to be removed from cache: $Key :
$RemovedReason"
}
# Build a cache, create a policy that references the callback and add items to the cache.
$MC = New-Object System.Runtime.Caching.MemoryCache('Main')
$Policy = New-Object System.Runtime.Caching.CacheItemPolicy
$Policy.AbsoluteExpiration = (Get-DAte).ToUniversalTime().AddSeconds(4) #N.B. It's always in UTC.
$Policy.RemovedCallback = $RemovedAction
$A = [PSCustomObject]@{ Name = 'Albert'; Age = 21 }
$B = [PSCustomObject]@{ Name = 'Bob'; Age = 21 }
[void]$MC.Set('A',$A, $Policy)
[void]$MC.Set('B',$B, $Policy)
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Green "Expect Bob to be expired immediately..."
[Void]$MC.Remove('B') # Expect to see Bob be expired.
Write-Host -ForegroundColor Green "Expect Albert to be expired due to the timeout, clearly it's in the cache at the start of the sleep."
$MC
Start-Sleep -Seconds 6
Write-Host -ForegroundColor Green "See, Albert has gone, as it's not in the cache, but it does not look as thouh the delegate was triggered."
$MC
这样做的结果是,删除“Albert”的显式示例按预期工作,并使用正确的参数调用处理程序,但是具有绝对过期时间的隐式示例似乎未按预期工作。
在后一种情况下,该项目似乎确实从缓存中过期(正如我们从它没有打印出来的事实中看到的那样),但是似乎回调确实没有被调用。
我怀疑这可能是线程问题,但我不清楚这里发生了什么。
这是实现自定义更改监视器回调的垫脚石。
最佳答案
这个答案晚了一年,但对于任何在这里找到方法的人来说 - 过期不会触发回调。数据过期后第一次调用数据会触发回调。请参阅MemoryCache AbsoluteExpiration acting strange
Start-Sleep -Seconds 6
$MC.Get('A') | out-null
Write-Host -ForegroundColor Green "See, Albert has gone, as it's not in the cache, but it does not look as thouh the delegate was triggered."
关于powershell - 如何使用 System.RuntimeCaching.MemoryCache 正确实现 PowerShell 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256489/