Session and Cache

Learn about special scopes in apps

Download this Lecture Docs

In this lecture, we look the session and cache scope for dashboards. Session scope is per-user storage for variables. If the user session expires or the dashboard is restarted, the session date will be reset. This is useful for adjusting dashboards based on user interaction.

The cache scope is used for storing variables across all users of a dashboard. This is useful for storing global data that many users can view but not necessarily change.

Be aware that session and cache scopes are not cleared automatically so storing large amounts of data will increase memory usage.

Example Code used in this lecture

New-UDDashboard -Title 'PowerShell Universal' -Content {
    New-UDTypography 'Dashboard 1'

    New-UDButton -Text 'Session' -OnClick {
        $Session:MySessionVariable = Get-Random
    }

    New-UDButton -Text 'Cache' -OnClick {
        $Cache:MyCacheVariable = Get-Random
    }

    New-UDAlert -Text "Session: $Session:MySessionVariable"
    New-UDAlert -Text "Cache: $Cache:MyCacheVariable"
}