Skip to content

Commit

Permalink
(chocolateyGH-90) Implemented Caching Package list to File
Browse files Browse the repository at this point in the history
The Get-ChocoInstalledPackage function pulls a new list from
'choco list -lo -r' costing measurable system impact when choco is
installing several packages.

This commit will allow the Installed package list to be cached locally
in the $env:ChocolateyInstall folder for 60 seconds before pulling a
new list.

# Conflicts:
#	DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
  • Loading branch information
jrdnr committed Dec 1, 2017
1 parent 8cd11a6 commit 249faf4
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions DSCResources/cChocoPackageInstall/cChocoPackageInstall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -396,26 +396,15 @@ Function Upgrade-Package {
}

function Get-ChocoInstalledPackage {
$res = $null
$PackageCacheSec = (Get-Date).AddSeconds('-60')
$childPath = 'ChocoInstalled.xml'
$ChocoInstallList = Join-Path -Path $env:ChocolateyInstall -ChildPath $childPath
$ChocoInstallList = Join-Path -Path $env:ChocolateyInstall -ChildPath 'ChocoInstalled.xml'

$lastWriteTime = (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime

if ( $PackageCacheSec -lt $lastWriteTime ) {
if ( $PackageCacheSec -lt (Get-Item $ChocoInstallList -ErrorAction SilentlyContinue).LastWriteTime ) {
$res = Import-Clixml $ChocoInstallList
} else {
choco list -lo -r | ConvertFrom-Csv -Header 'Name', 'Version' -Delimiter "|" -OutVariable res | Export-Clixml -Path $ChocoInstallList
}
if (-not $res){
$res = choco list -lo | ForEach-Object {
$Obj = $_ -split '\s'
[pscustomobject]@{
'Name' = $Obj[0]
'Version' = $Obj[1]
}
}
Export-Clixml -path $ChocoInstallList -InputObject $res
}

Return $res
}

Expand Down

0 comments on commit 249faf4

Please sign in to comment.