> 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/es/plataforma/cache.md).

# Caché

## Caché a nivel de servidor

PowerShell Universal ofrece la posibilidad de utilizar una caché a nivel de servidor para almacenar datos que se usan entre APIs, Automation y Dashboards. Puede configurar el tiempo de vida de los elementos de la caché mediante el cmdlet `Set-PSUCache` en cualquier script de PowerShell que ejecute en PowerShell Universal. También puede recuperar elementos de la caché usando `Get-PSUCache`.

Algunos ejemplos de usos de la caché pueden ser:

* Recopilar datos con un trabajo programado y mostrarlos en un dashboard
* Recopilar datos de una API y utilizarlos en un trabajo
* Recopilar datos mediante la entrada de un dashboard y ponerlos en cola para ejecutarlos en un trabajo programado

#### Establecer elementos en la caché

Para establecer elementos en la caché, puede utilizar `Set-PSUCache`. Los elementos de la caché se serializan en cadenas mediante CLIXML y el serializador de PowerShell. Cuando recupere objetos de la caché, ya no serán objetos activos.

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

Existen tres tipos de técnicas de invalidación de caché que puede emplear.

#### Expiración absoluta

El parámetro `AbsoluteExpiration` define en qué momento se invalida el elemento de la caché.

El siguiente ejemplo invalida el elemento de la caché después de 10 minutos.

```powershell
Set-PSUCache -Key "CurrentDate" -Value (Get-Date) -AbsoluteExpiration (Get-Date).AddMinutes(10)
```

#### Expiración absoluta a partir de ahora

El parámetro `AbsoluteExpirationFromNow` define cuándo se invalida un elemento de la caché utilizando un TimeSpan en lugar de un DateTime calculado.

El siguiente elemento de la caché se invalida 1 hora a partir de ahora.

```powershell
Set-PSUCache -Key "CurrentDate" -Value (Get-Date) -AbsoluteExpirationFromNow ([TimeSpan]::FromHours(1))
```

#### Expiración deslizante

El parámetro `SlidingExpiration` le permite definir el tiempo que transcurre antes de que se invalide el elemento de la caché. Cada vez que se accede al elemento de la caché, el temporizador se reinicia.

El siguiente elemento de la caché se invalidará en 5 minutos. Si se accede a él dentro de esos 5 minutos, se reiniciará durante otros 5 minutos.

```powershell
Set-PSUCache -Key "CurrentDate" -Value (Get-Date) -SlidingExpiration ([TimeSpan]::FromMinutes(5))
```

## Obtener elementos de la caché

Puede utilizar el cmdlet `Get-PSUCache` para recuperar elementos de la caché. Solo tiene que proporcionar la clave del elemento que desea recuperar. El cmdlet devolverá el objeto deserializado.

```powershell
Get-PSUCache -Key "CurrentDate"
```

## Caché persistente

Puede utilizar el parámetro `-Persist` de `Set-PSUCache` para almacenar datos en la base de datos. Esto permite el almacenamiento en caché distribuido y una caché que sobrevivirá a los reinicios del servicio de PowerShell Universal.

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

### Vaciar la caché

Puede eliminar elementos de la caché usando `Remove-PSUCache` o `Clear-PSUCache` .

```powershell
# Remove a specific item
Remove-PSUCache -Key 'Key123'

# Clear the entire cache
Clear-PSUCache
```

## Ámbito $Cache

Las APIs, los scripts de Automation y los Dashboards admiten todos un ámbito $Cache. Este ámbito se utiliza para almacenar en caché datos entre runspaces que persistirán en la memoria de cada uno de los entornos de ejecución.

Por ejemplo, si establece una variable $Cache en un dashboard, no estará disponible en una API. Esto no es así cuando se utiliza el entorno Integrated. Si un dashboard y una API utilizan ambos el entorno integrado, comparten el ámbito de caché.

Puede establecer y recuperar datos del ámbito de caché utilizando la sintaxis de asignación y recuperación de variables.

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

Ámbito `$Cache` frente a caché a nivel de servidor: el ámbito `$Cache` difiere de la caché a nivel de servidor, ya que solo reside en el entorno de ejecución de la función que esté utilizando. Son diferentes y no se puede usar uno para obtener el otro.

## Ver la caché

Puede ver la caché a nivel de servidor en la consola de administración navegando a Platform \ Cache.

<figure><img src="/files/EGrtmBsykzmjGPQLii29" alt=""><figcaption><p>Ver datos en caché</p></figcaption></figure>

## API

* [Get-PSUCache](/powershell-universal/es/comandos-de-powershell/get-psucache.md)
* [Set-PSUCache](/powershell-universal/es/comandos-de-powershell/set-psucache.md)
* [Remove-PSUCache](/powershell-universal/es/comandos-de-powershell/remove-psucache.md)
* [Clear-PSUCache](/powershell-universal/es/comandos-de-powershell/clear-psucache.md)


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
