|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +$script:CurrentCacheSchemaVersion = 1 |
| 5 | + |
4 | 6 | function Write-DscTrace { |
5 | 7 | param( |
6 | 8 | [Parameter(Mandatory = $false)] |
@@ -242,47 +244,53 @@ function Invoke-DscCacheRefresh { |
242 | 244 | "Reading from Get-DscResource cache file $cacheFilePath" | Write-DscTrace |
243 | 245 |
|
244 | 246 | $cache = Get-Content -Raw $cacheFilePath | ConvertFrom-Json |
245 | | - $dscResourceCacheEntries = $cache.ResourceCache |
246 | 247 |
|
247 | | - if ($dscResourceCacheEntries.Count -eq 0) { |
248 | | - # if there is nothing in the cache file - refresh cache |
| 248 | + if ($cache.CacheSchemaVersion -ne $script:CurrentCacheSchemaVersion) { |
249 | 249 | $refreshCache = $true |
| 250 | + "Incompatible version of cache in file '"+$cache.CacheSchemaVersion+"' (expected '"+$script:CurrentCacheSchemaVersion+"')" | Write-DscTrace |
| 251 | + } else { |
| 252 | + $dscResourceCacheEntries = $cache.ResourceCache |
250 | 253 |
|
251 | | - "Filtered DscResourceCache cache is empty" | Write-DscTrace |
252 | | - } |
253 | | - else |
254 | | - { |
255 | | - "Checking cache for stale entries" | Write-DscTrace |
| 254 | + if ($dscResourceCacheEntries.Count -eq 0) { |
| 255 | + # if there is nothing in the cache file - refresh cache |
| 256 | + $refreshCache = $true |
256 | 257 |
|
257 | | - foreach ($cacheEntry in $dscResourceCacheEntries) { |
258 | | - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace |
| 258 | + "Filtered DscResourceCache cache is empty" | Write-DscTrace |
| 259 | + } |
| 260 | + else |
| 261 | + { |
| 262 | + "Checking cache for stale entries" | Write-DscTrace |
259 | 263 |
|
260 | | - $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { |
261 | | - |
262 | | - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) |
263 | | - { |
264 | | - "Detected stale cache entry '$($_.Name)'" | Write-DscTrace |
265 | | - $refreshCache = $true |
266 | | - break |
| 264 | + foreach ($cacheEntry in $dscResourceCacheEntries) { |
| 265 | + #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace |
| 266 | + |
| 267 | + $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { |
| 268 | + |
| 269 | + if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) |
| 270 | + { |
| 271 | + "Detected stale cache entry '$($_.Name)'" | Write-DscTrace |
| 272 | + $refreshCache = $true |
| 273 | + break |
| 274 | + } |
267 | 275 | } |
268 | | - } |
269 | 276 |
|
270 | | - if ($refreshCache) {break} |
271 | | - } |
| 277 | + if ($refreshCache) {break} |
| 278 | + } |
272 | 279 |
|
273 | | - "Checking cache for stale PSModulePath" | Write-DscTrace |
| 280 | + "Checking cache for stale PSModulePath" | Write-DscTrace |
274 | 281 |
|
275 | | - $m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue} |
| 282 | + $m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue} |
276 | 283 |
|
277 | | - $hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths) |
278 | | - $hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName) |
279 | | - $hs_cache.SymmetricExceptWith($hs_live) |
280 | | - $diff = $hs_cache |
| 284 | + $hs_cache = [System.Collections.Generic.HashSet[string]]($cache.PSModulePaths) |
| 285 | + $hs_live = [System.Collections.Generic.HashSet[string]]($m.FullName) |
| 286 | + $hs_cache.SymmetricExceptWith($hs_live) |
| 287 | + $diff = $hs_cache |
281 | 288 |
|
282 | | - "PSModulePath diff '$diff'" | Write-DscTrace |
| 289 | + "PSModulePath diff '$diff'" | Write-DscTrace |
283 | 290 |
|
284 | | - if ($diff.Count -gt 0) { |
285 | | - $refreshCache = $true |
| 291 | + if ($diff.Count -gt 0) { |
| 292 | + $refreshCache = $true |
| 293 | + } |
286 | 294 | } |
287 | 295 | } |
288 | 296 | } |
@@ -330,6 +338,7 @@ function Invoke-DscCacheRefresh { |
330 | 338 | $cache.ResourceCache = $dscResourceCacheEntries |
331 | 339 | $m = $env:PSModulePath -split [IO.Path]::PathSeparator | %{Get-ChildItem -Directory -Path $_ -Depth 1 -ea SilentlyContinue} |
332 | 340 | $cache.PSModulePaths = $m.FullName |
| 341 | + $cache.CacheSchemaVersion = $script:CurrentCacheSchemaVersion |
333 | 342 |
|
334 | 343 | # save cache for future use |
335 | 344 | # TODO: replace this with a high-performance serializer |
@@ -494,6 +503,7 @@ class dscResourceCacheEntry { |
494 | 503 | } |
495 | 504 |
|
496 | 505 | class dscResourceCache { |
| 506 | + [int] $CacheSchemaVersion |
497 | 507 | [string[]] $PSModulePaths |
498 | 508 | [dscResourceCacheEntry[]] $ResourceCache |
499 | 509 | } |
|
0 commit comments