Use of $Cache in my own module

Product: PowerShell Universal
Version: 3.5.5

Just to verify, I cannot use $Cache:variable inside my own module’s functions. Is that correct?
Instead, I must find a way to bring the data I want to cache into the pages ps1’s.

Just confirming
Thank you.

I think you should be able to. The $Cache is a provider so as long as it’s loaded into the session it should be accessible.

Thanks @adam I am trying to track progress. Here is an example that I can’t seem to increment past 0

function Get-GacAFUserAll {
    [CmdletBinding()]
    param (

        [Parameter()]
        $ThrottleLimit = 8
    )



    @(0..5) | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {

        foreach ($item in (0..10000)) {

            $Cache:sync = $using:Cache:sync
            $Cache:sync++

            Start-Sleep -Milliseconds (Get-Random -Minimum 100 -Maximum 500)

            # Output
            [PSCustomObject]@{
                item = $item
            }
        }
    }
}
New-UDPage -Name 'Load Tenant Data' -Title 'Load Tenant Data' -Url loadtenantdata -Content {
    $Cache:sync = 0
    New-UDDynamic -Id 'date' -Content {
        New-UDTypography -Text "$($Cache:sync)" -Variant h3
        New-UDTypography -Text "$(Get-Random)" -Variant h3
    } -AutoRefresh -AutoRefreshInterval 1

How would I save state across runspaces?

Perhaps Endpoints - PowerShell Universal

?

I have no idea why the cache isn’t working that foreach-object parallel but here’s a workaround. It accesses the cache directly and bypasses the provider.

function Get-GacAFUserAll {
    [CmdletBinding()]
    param (

        [Parameter()]
        $ThrottleLimit = 8
    )

    

    @(0..5) | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel {

        foreach ($item in (0..10000)) {
            $CurrentValue = [UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache['sync']
            $CurrentValue++
            [UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache['sync'] = $CurrentValue

            Start-Sleep -Milliseconds (Get-Random -Minimum 100 -Maximum 500)

            # Output
            [PSCustomObject]@{
                item = $item
            }
        } 
    } 
}

New-UDDashboard -Title 'PowerShell Universal' -Content {
    [UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Remove("sync")
    [UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Add("sync", 1)

    New-UDButton -Text 'Start' -OnClick {
        Get-GacAFUserAll
    }
    New-UDDynamic -Id 'date' -Content {
        $var = [UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache['sync']
        New-UDTypography -Text $var -Variant h3
        New-UDTypography -Text "$(Get-Random)" -Variant h3
    } -AutoRefresh -AutoRefreshInterval 1
}
1 Like

This still would not work with the stepper. testing with the bare bones example worked, however. I am just going to remove the stepper (which is fine) and start over from scratch my entire site with 3.6.2 as AT&T won’t open port 445 and I am still seeing table layout anomalies ( and I cant seem to comment out themes without error - I am not sure what files are native and what I copied from cl’s repo prior to being officially incorporated into PSU). Either way, this gives me an opportunity to start fresh - I just need to verify the instructions to use for the Git SQL integration.

Happy New Year!
@adam I am running into an issue with 3.6.4 where adding:

[UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Remove("sync")
[UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Add("sync", 1)

gives me the following errors (and dash doesn’t auto start - I can manually start it) when I push a commit:

Is the ForEach-Object -Parallel issue for $Session: (and $Cache:) planned for the next release? IIRC, since $Cache: is system wide, this band-aid works for only one user. After that, the variables begin stepping on each other any time another user uses the dashboard.

Code (if I comment out lines 34 and 35 the dashboard starts fine):

No idea why this didn’t cause this problem before but it looks like Remove returns a bool. Try piping that to Out-Null.

Cache.Remove('sync') | Out-Null

fixed thank you!