Skip to content

Commit

Permalink
cache: add Get-CacheList method re #1
Browse files Browse the repository at this point in the history
  • Loading branch information
qbikez committed Jul 8, 2019
1 parent 144e6eb commit 566de5f
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 176 deletions.
12 changes: 5 additions & 7 deletions Cache/src/Cache.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
$helpersPath = (Split-Path -parent $MyInvocation.MyCommand.Definition)

. "$helpersPath\imports.ps1"

Export-ModuleMember -Function * -Alias *


$helpersPath = (Split-Path -parent $MyInvocation.MyCommand.Definition)

. "$helpersPath\imports.ps1"

Export-ModuleMember -Function * -Alias *
198 changes: 132 additions & 66 deletions Cache/src/functions/Cache.ps1
Original file line number Diff line number Diff line change
@@ -1,66 +1,132 @@
function _SanitizeContainerName([Parameter(Mandatory=$true, ValueFromPipeline=$true)]$container) {
return $container.Replace("\","_").Replace("/","_").Replace(":","_").Replace("?","_")
}

function Export-Cache([Parameter(Mandatory=$true,ValueFromPipeline=$true)]$data, [Parameter(Mandatory=$true)]$container, [Parameter(Mandatory=$false)]$dir = ".cache") {
# check custom providers
if ($container.Contains(":")) {
$splits = $container.split(":")
$provider = $splits[0]
if ($null -ne (get-command "export-$($provider)cache" -ErrorAction Ignore)) {
return & "export-$($provider)cache" $data $container.Substring($provider.length + 1)
}
}
# default disk cache provider
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }
$container = _SanitizeContainerName $container
try {
if (!(test-path $cacheDir)) { $null = new-item -ItemType directory $cacheDir -erroraction stop }
} catch {
throw "could not find or create cache directory '$cachedir'"
}
$path = "$cacheDir\$container.json"
$data | ConvertTo-Json | Out-File $path -Encoding utf8
}

function Import-Cache([Parameter(Mandatory=$true)]$container, [Parameter(Mandatory=$false)]$dir = ".cache") {
# check custom providers
if ([string]::IsNullOrEmpty($container)) { throw "container cannot be null or empty" }
if ($container.Contains(":")) {
$splits = $container.split(":")
$provider = $splits[0]
if ($null -ne (get-command "import-$($provider)cache" -ErrorAction Ignore)) {
return & "import-$($provider)cache" $container.Substring($provider.length + 1)
}
}
# default disk cache provider
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }
$container = _SanitizeContainerName $container
try {
if (!(test-path $cacheDir)) { $null = new-item -ItemType directory $cacheDir -erroraction stop }
} catch {
throw "could not find or create cache directory '$cachedir'"
}
$path = "$cacheDir\$container.json"

$data = $null
if (test-path $path) {
$data = Get-Content $path -Encoding UTF8 | out-String | ConvertFrom-Json
}

return $data
}

function Remove-Cache([Parameter(Mandatory=$true)]$container, [Parameter(Mandatory=$false)]$dir = ".cache") {
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }
$container = _SanitizeContainerName $container
if ((test-path $cacheDir)) {
$path = "$cacheDir\$container.json"
if (test-path $path) {
remove-item $path
}
}
}
function Export-Cache(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]$data,
[Parameter(Mandatory = $true)]$container,
[Parameter(Mandatory = $false)]$dir = ".cache"
) {
# try custom providers
if (_IsCustomProvider $container) {
return _InvokeCustomProviderCommand -command "export" -container $container -data $data
}
else {
# default disk cache provider
$container = _SanitizeContainerName $container

if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }

try {
if (!(test-path $cacheDir)) { $null = new-item -ItemType directory $cacheDir -erroraction stop }
}
catch {
throw "could not find or create cache directory '$cachedir'"
}
$path = "$cacheDir\$container.json"
$data | ConvertTo-Json | Out-File $path -Encoding utf8
}
}

function Import-Cache {
param (
[Parameter(Mandatory = $true)]$container,
[Parameter(Mandatory = $false)]$dir = ".cache"
)
if ([string]::IsNullOrEmpty($container)) { throw "container cannot be null or empty" }
# try custom providers
if (_IsCustomProvider $container) {
return _InvokeCustomProviderCommand -command "export" -container $container
}
else {
# default disk cache provider
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }

$container = _SanitizeContainerName $container
try {
if (!(test-path $cacheDir)) { $null = new-item -ItemType directory $cacheDir -erroraction stop }
}
catch {
throw "could not find or create cache directory '$cachedir'"
}
$path = "$cacheDir\$container.json"

$data = $null
if (test-path $path) {
$data = Get-Content $path -Encoding UTF8 | out-String | ConvertFrom-Json
}

return $data
}
}

function Get-CacheList {
param (
[Parameter(Mandatory = $false)]$dir = ".cache"
)

if ([string]::IsNullOrEmpty($dir)) { throw "container cannot be null or empty" }
# try custom providers
if (_IsCustomProvider $drir) {
return _InvokeCustomProviderCommand -command "list" -dir $dir
}
else {
# default disk cache provider
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }

if (!(test-path $cacheDir)) { return @{} }

$result = @{}
$containers = Get-ChildItem $cacheDir -Filter "*.json"

foreach($container in $containers) {
$key = [System.IO.Path]::GetFileNameWithoutExtension($container.name)
$value = Get-Content $container.fullname | out-string
$result[$key] = $value
}

return $result
}
}

function Remove-Cache {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $true)]$container,
[Parameter(Mandatory = $false)]$dir = ".cache"
)
if ([System.IO.Path]::IsPathRooted($dir)) { $cacheDir = $dir }
else { $cacheDir = Join-Path "$home\Documents\windowspowershell" $dir }
$container = _SanitizeContainerName $container
if ((test-path $cacheDir)) {
$path = "$cacheDir\$container.json"
if (test-path $path) {
if ($PSCmdlet.ShouldProcess("Remove cache container", "Remove $path")) {
remove-item $path
}
}
}
}

function _IsCustomProvider($container) {
return $container -ne $null -and $container.Contains(":")
}

function _InvokeCustomProviderCommand($command, $container, $dir, $data) {
$splits = $container.split(":")
$provider = $splits[0]
$providerPath = $splits[1]
if ($null -ne (get-command "export-$($provider)cache" -ErrorAction Ignore)) {
$p = @{
container = $providerPath
}
if ($data -ne $null) {
$p.data = $data
}

return (& "$command-$($provider)cache" @p)
}
}

function _SanitizeContainerName([Parameter(Mandatory = $true, ValueFromPipeline = $true)]$container) {
return $container.Replace("\", "_").Replace("/", "_").Replace(":", "_").Replace("?", "_")
}
Loading

0 comments on commit 566de5f

Please sign in to comment.