如何使用System.RuntimeCaching.MemoryCache正确实现PowerShell回调

时间:2017-07-22 15:57:10

标签: powershell events caching delegates

我正在研究如何使用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”的显式示例按预期工作,并使用正确的参数调用处理程序,但是具有绝对到期时间的隐式示例似乎没有按预期工作。

在后一种情况下,似乎该项目从缓存中过期(正如我们可以看到它没有被打印出来的那样)但是看起来回调确实没有被调用。

我怀疑这可能是一个线程问题,但我不清楚这里发生了什么。

这是实现自定义更改监视器回调的跳板。

1 个答案:

答案 0 :(得分:1)

这个答案已经晚了一年,但是对于任何在这里找到答案的人来说,到期不会触发回调。数据过期后第一次调用该数据将触发回调。参见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."