Couple questions about $Cache:variables

I am finding a $Cache:variable keeps getting larger and larger has my endpoint populates this $Cache:variable.

  • How do you Clear these $Cache:variables periodically?
  • are these variables a powershell thing or PSU related?

Thanks in advance.

Product: PowerShell Universal
Version: 1.4.6

The Cache provider is Universal specific. You should be able to use cmdlets like Remove-Item to clear out specific variables. You can also access the cache dictionary directly to clear the entire thing.

[UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Clear()

Thanks Adam, So If I had a cache variable named $Cache:datalist i would do a $Cache:datalist::cache.clear()

If you want to clear out a specific variable, you can just set it to null and it will no longer contain the cached data:

$Cache:Datalist = $null

If you want to completely remove the record from the cache (including the key), you can clear the item from the cache directly. It won’t really save any more memory unless you have millions of items in the cache.

[UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Remove("datalist")

If you want to completely remove everything stored in the cache, then use Clear().

[UniversalDashboard.Execution.GlobalCachedVariableProvider]::Cache.Clear()
1 Like

Thanks Adam.

We are still a litte confused by the Cache variable. When we use Cache:datalist and add a number of values to this variable that are used to display on the dashboard, and if we do any restarts of the dashboard in the admin portal, I start seeing duplicate values.

Using $Cache:datalist = $null does not clear the variable per say but makes it null and I get a bunch of these errors

You cannot call a method on a null-valued expression.

Here is a code snip

$Cache:datalist = [System.Collections.Generic.List[pscustomobject]]::new()
.
.
.
New-UDEndpoint -Schedule $BuildRefresh -Endpoint {
      <processing logic>
     $Cache:datalist.Add(data)
}

When I restart PSU Server (recycle service) then the data is fresh and I only see singular values for different data elements which is correct. when I restart the dashboard a few times in the admin portal I start seeing duplicates.

What I am trying to do is prevent the duplicates that show up when I restart the dashboard a few times without clearing the Cache:datalist.

I am wondering if I need to do something like this (not pure powershell)

if exists (Cache:datalist(data))  {
     Cache:datalist.Replace(data)
} else {
     Cache:datalist.Add(data)
}

It makes me think that New-UDEndpoint is running more than once when doing restarts. @DataTraveler - Can you open an issue to so that we can investigate this?

So when you restart the dashboard should it clear the cache:variables that are used in that dashboard?