Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds inbuilt support for caching values #1197

Merged
merged 5 commits into from
Dec 13, 2023
Merged

Adds inbuilt support for caching values #1197

merged 5 commits into from
Dec 13, 2023

Conversation

Badgerati
Copy link
Owner

Description of the Change

Adds inbuilt support for caching values, both via functions and a new $cache: scoped variable. The inbuilt cache stores values in-memory with a default TTL of 1hr (can be overridden), and there is also support for adding custom storage as well

The following new functions have been added:

  • Get-PodeCache
  • Set-PodeCache
  • Test-PodeCache
  • Remove-PodeCache
  • Clear-PodeCache
  • Add-PodeCacheStorage
  • Remove-PodeCacheStorage
  • Get-PodeCacheStorage
  • Test-PodeCacheStorage
  • Set-PodeCacheDefaultStorage
  • Get-PodeCacheDefaultStorage
  • Set-PodeCacheDefaultTtl
  • Get-PodeCacheDefaultTtl

Related Issue

Resolves #1184

Examples

Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
    # check cache
    $cpu = $cache:cpu
    if ($null -ne $cpu) {
        Write-PodeJsonResponse -Value @{ CPU = $cpu }
        return
    }
    # get cpu, and cache for 1hr
    $cache:cpu = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
    Write-PodeJsonResponse -Value @{ CPU = $cache:cpu }
}

or the same using $cache: instead:

Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
    # check cache
    $cpu = $cache:cpu
    if ($null -ne $cpu) {
        Write-PodeJsonResponse -Value @{ CPU = $cpu }
        return
    }
    # get cpu, and cache for 1hr
    $cache:cpu = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
    Write-PodeJsonResponse -Value @{ CPU = $cache:cpu }
}

@Badgerati Badgerati added this to the 2.10.0 milestone Dec 10, 2023
@Badgerati Badgerati self-assigned this Dec 10, 2023
@Badgerati Badgerati merged commit 9c71d26 into develop Dec 13, 2023
8 checks passed
@Badgerati Badgerati deleted the Issue-1184 branch December 13, 2023 08:30
@Badgerati Badgerati mentioned this pull request Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inbuilt caching support
1 participant