> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/powershell-universal/platform/cache.md).

# Cache

Use the PowerShell Universal server-level cache with Set-PSUCache and Get-PSUCache, covering expiration types, persistent caching, and the Cache scope.

PowerShell Universal provides a server-level cache for sharing data between APIs, automation, and apps. Set cache entries with `Set-PSUCache` and retrieve them with `Get-PSUCache`.

Cache values are serialized with the PowerShell serializer. Retrieved objects are deserialized objects rather than live instances.

```powershell
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date)
Get-PSUCache -Key 'CurrentDate'
```

## Expiration

Cache items support absolute, relative, and sliding expiration.

```powershell
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -AbsoluteExpiration (Get-Date).AddMinutes(10)
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(1))
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -SlidingExpiration ([TimeSpan]::FromMinutes(5))
```

## Persistent cache

Use `-Persist` to store a cache entry in the PowerShell Universal database. Persistent entries survive service restarts and are available to connected servers that use the same database.

```powershell
Set-PSUCache -Key 'CurrentDate' -Value (Get-Date) -Persist
```

### Encrypt persistent values

Use `-Encrypt` with `-Persist` to encrypt the serialized cache value before it is stored in the database. PowerShell Universal decrypts the value automatically when `Get-PSUCache` retrieves it.

```powershell
Set-PSUCache -Key 'ApiResponse' -Value $response -Persist -Encrypt

$response = Get-PSUCache -Key 'ApiResponse'
```

`-Encrypt` requires `-Persist`; it cannot encrypt memory-only cache entries. Use encrypted persistent cache for sensitive values that must be shared across servers or retained after restarts. Continue to limit access to sensitive cache data with appropriate permissions and reader roles.

## Remove cache entries

```powershell
Remove-PSUCache -Key 'CurrentDate'
Clear-PSUCache
```

## $Cache scope

APIs, automation scripts, and dashboards support `$Cache:` variables for in-memory data shared across runspaces in the same execution environment.

```powershell
$Cache:MyValue = 'Hello'
Write-Host $Cache:MyValue
```

`$Cache:` is separate from the server-level cache. It is limited to the execution environment; for example, a dashboard cache value is not available to an API unless both use the Integrated environment.

## View the cache

View server-level cache entries under **Manage > Cache**.

## API

* `Get-PSUCache`
* `Set-PSUCache`
* `Remove-PSUCache`
* `Clear-PSUCache`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.devolutions.net/powershell-universal/platform/cache.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
