Main | Contact | Blog | Documentation

PowerShell Framework

The project dedicated to empowering your PowerShell scripting.

Scenario: Persisted Cache

Synopsis

A strategic guide to implementing the persisted cache feature.

Description

The configuration system can be used to manage data caches. It offers a simple abstraction to handle import/export of cached settings. It also offers access to that cache across the entire process, irrespective of scope or runspace.

Strengths

Weaknesses

Implementation

The implementation consists of four basic steps:

Example

# Initializing settings
Set-PSFConfig -Module MyModule -Name Example1 -Value 42 -Validation integer -Initialize -Description "Some arbitrary example setting that will not be part of the cache"
Set-PSFConfig -Module MyModule -Name Example2 -Value $true -Validation bool -Initialize -Description "Some arbitrary example setting that will not be part of the cache"

Set-PSFConfig -Module MyModule -Name Example3 -Value @() -ModuleExport -Hidden -Initialize -Description "Some arbitrary example setting that WILL be part of the cache"
Set-PSFConfig -Module MyModule -Name Example4 -Value @() -ModuleExport -Hidden -Initialize -Description "Some arbitrary example setting that WILL be part of the cache"
Set-PSFConfig -Module MyModule -Name Example5 -Value @() -ModuleExport -Hidden -Initialize -Description "Some arbitrary example setting that WILL be part of the cache"

# Importing existing cache
Import-PSFConfig -ModuleName MyModule

# Access cache value in code
Get-PSFConfigValue -FullName MyModule.Example3

# (later)Exporting the current in-memory settings marked for cache-persistence
Export-PSFConfig -ModuleName MyModule

Resources

It’s a runspace world

Using the cache in a runspaced scenario generally risks conflict, since each import in each runspace overwrites the current cache values with the persisted ones. There are two basic ways to handle this:

Notes

Back to Configuration

Version 1.0
Written on: 2018-05-24
Updated on: 2018-06-07