mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
Added support to create and load cache data (prototype)
This commit is contained in:
parent
d7eedea277
commit
5f4c788861
2 changed files with 62 additions and 0 deletions
30
lib/core/cache/Get-IcingaCacheData.psm1
vendored
Normal file
30
lib/core/cache/Get-IcingaCacheData.psm1
vendored
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
function Get-IcingaCacheData()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Space,
|
||||||
|
[string]$CacheStore,
|
||||||
|
[string]$KeyName
|
||||||
|
);
|
||||||
|
|
||||||
|
$CacheFile = Join-Path -Path (Join-Path -Path (Join-Path -Path (Get-IcingaCacheDir) -ChildPath $Space) -ChildPath $CacheStore) -ChildPath ([string]::Format('{0}.json', $KeyName));
|
||||||
|
[string]$Content = '';
|
||||||
|
$cacheData = @{};
|
||||||
|
|
||||||
|
if ((Test-Path $CacheFile) -eq $FALSE) {
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$Content = Get-Content -Path $CacheFile;
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($Content)) {
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cacheData = ConvertFrom-Json -InputObject ([string]$Content);
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($KeyName)) {
|
||||||
|
return $cacheData;
|
||||||
|
} else {
|
||||||
|
return $cacheData.$KeyName;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
lib/core/cache/Set-IcingaCacheData.psm1
vendored
Normal file
32
lib/core/cache/Set-IcingaCacheData.psm1
vendored
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
function Set-IcingaCacheData()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Space,
|
||||||
|
[string]$CacheStore,
|
||||||
|
[string]$KeyName,
|
||||||
|
$Value
|
||||||
|
);
|
||||||
|
|
||||||
|
$CacheFile = Join-Path -Path (Join-Path -Path (Join-Path -Path (Get-IcingaCacheDir) -ChildPath $Space) -ChildPath $CacheStore) -ChildPath ([string]::Format('{0}.json', $KeyName));
|
||||||
|
$cacheData = @{};
|
||||||
|
|
||||||
|
if ((Test-Path $CacheFile)) {
|
||||||
|
$cacheData = Get-IcingaCacheData -Space $Space -CacheStore $CacheStore;
|
||||||
|
} else {
|
||||||
|
New-Item -Path $CacheFile -Force | Out-Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($null -eq $cacheData -or $cacheData.Count -eq 0) {
|
||||||
|
$cacheData = @{
|
||||||
|
$KeyName = $Value
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
if ($cacheData.PSobject.Properties.Name -ne $KeyName) {
|
||||||
|
$cacheData | Add-Member -MemberType NoteProperty -Name $KeyName -Value $Value -Force;
|
||||||
|
} else {
|
||||||
|
$cacheData.$KeyName = $Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-Content -Path $CacheFile -Value (ConvertTo-Json -InputObject $cacheData -Depth 100) | Out-Null;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue