Set-PSUCache allows for multiple values

Product: PowerShell Universal
Version: 5.4.4

Set-PSUCache allows for multiple values with the same key, is this intended?

1. Violates the core principle of key-value stores

  • The cache is fundamentally a key-value store, and each key should be unique. Allowing multiple values for the same key breaks this model and can cause confusion, inconsistency, and unexpected behavior.

2. Unpredictable behavior

  • When multiple values exist for the same key:
    • Which one is returned when you call Get-Cache?
    • Are values returned in order of insertion, time, or priority?
    • What happens if you update the cache — does it overwrite one, all, or none?
  • This ambiguity leads to bugs that are hard to trace and debug.

3. Data inconsistency and logic errors

  • Inconsistent cache values for the same key can cause business logic failures, especially in multi-user dashboards or background jobs that depend on accurate, current state information.

4. Makes the cache harder to manage and debug

  • Imagine trying to troubleshoot a value mismatch and realizing there are several values stored for the same key. This creates unnecessary complexity for developers and support teams.

5. Performance and memory overhead

  • Caching is meant to improve performance, but storing multiple values under the same key increases memory usage and defeats the efficiency gains you’d expect from a cache.

6. Undermines concurrency safety

  • PowerShell Universal apps often run in multi-threaded or concurrent environments (dashboards, APIs, scheduled jobs). If multiple values can exist under a single key, race conditions and data corruption become more likely.

7. Misleads developers

  • Developers expect Set-Cache -Key "MyKey" -Value "abc" to replace the previous value. If instead it silently adds another value for the same key, that’s counter-intuitive and leads to incorrect assumptions in code.

Get-PSUCache shouldn’t be allowing multiple values per key. I just did this test and it behaves as I would expect.

Set-PSUCache -Key '123' -Value '123'
Set-PSUCache -Key '123' -Value '456'
Set-PSUCache -Key '123' -Value '789'
Get-PSUCache -Key '123'

Set-PSUCache -Key 'xyz' -Value '123' -Persist
Set-PSUCache -Key 'xyz' -Value '456' -Persist
Set-PSUCache -Key 'xyz' -Value '789' -Persist
Get-PSUCache -Key 'xyz'

Get-PSUCache -List

# Output

789
789
Key                             : xyz
Value                           : <Objs Version="1.1.0.1" xmlns="http://schemas
                                  .microsoft.com/powershell/2004/04">
                                    <S>789</S>
                                  </Objs>
AbsoluteExpiration              : 
AbsoluteExpirationRelativeToNow : 
SlidingExpiration               : 
Persist                         : True
Roles                           : 
SecurityContextId               : 00000000-0000-0000-0000-000000000000
LastRead                        : 4/4/2025 1:41:49 PM
Created                         : 4/4/2025 1:41:23 PM
Updated                         : 4/4/2025 1:41:49 PM
Id                              : 0
Key                             : 123
Value                           : <Objs Version="1.1.0.1" xmlns="http://schemas
                                  .microsoft.com/powershell/2004/04">
                                    <S>789</S>
                                  </Objs>
AbsoluteExpiration              : 
AbsoluteExpirationRelativeToNow : 
SlidingExpiration               : 
Persist                         : False
Roles                           : 
SecurityContextId               : 00000000-0000-0000-0000-000000000000
LastRead                        : 4/4/2025 1:41:49 PM
Created                         : 4/4/2025 1:40:57 PM
Updated                         : 4/4/2025 1:41:49 PM
Id                              : 0

Is this chatgpt?

Did you avoid setting same Key with -persist on and off on purpose :D? Because this is exactly the problem. And when I want to remove one it always defaults to the oldest one

Set-PSUCache -Key 'Example' -value '123'
Set-PSUCache -Key 'Example' -value '123' -Persist
Get-PSUCache -key 'Example'
123
Get-PSUCache -List

Key                             : Example
Value                           : <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
                                    <S>123</S>
                                  </Objs>
AbsoluteExpiration              :
AbsoluteExpirationRelativeToNow :
SlidingExpiration               :
Persist                         : True
Roles                           :
SecurityContextId               : 00000000-0000-0000-0000-000000000000
LastRead                        : 2025-04-07 07:09:33
Created                         : 2025-04-07 07:09:12
Updated                         : 2025-04-07 07:09:12
Id                              : 0

Key                             : Example
Value                           : <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
                                    <S>123</S>
                                  </Objs>
AbsoluteExpiration              :
AbsoluteExpirationRelativeToNow :
SlidingExpiration               :
Persist                         : False
Roles                           :
SecurityContextId               : 00000000-0000-0000-0000-000000000000
LastRead                        : 2025-04-07 07:09:47
Created                         : 2025-04-07 07:09:02
Updated                         : 2025-04-07 07:09:02
Id                              : 0

Dont know how this is relevant but it helps me write in English since its my 2nd language

I understand the issue now. The problem is that we don’t clear the non-persistent item when settings the same key for a persistent item.

Great thanks :slight_smile: