< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrCacheMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCacheMiddleware.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 145
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/13/2025 - 17:19:56 Line coverage: 0% (0/27) Total lines: 145 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b10/13/2025 - 16:52:37 Line coverage: 0% (0/25) Total lines: 145 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrCacheMiddleware.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Adds response caching to the Kestrun server.
 4    .DESCRIPTION
 5        This cmdlet allows you to enable and configure response caching for the Kestrun server.
 6        It can be used to improve performance by caching responses for frequently requested resources.
 7    .PARAMETER Server
 8        The Kestrun server instance to which response caching will be added.
 9    .PARAMETER SizeLimit
 10        The maximum size, in bytes, of the response cache. If not specified, the default
 11        size limit of the underlying implementation will be used.
 12    .PARAMETER MaximumBodySize
 13        The maximum size, in bytes, of the response body that can be cached. If not specified,
 14        the default value of 64KB will be used.
 15    .PARAMETER UseCaseSensitivePaths
 16        If specified, the caching will be case-sensitive with respect to request paths.
 17
 18    .PARAMETER NoCache
 19        If specified, the 'no-cache' directive will be added to the Cache-Control header.
 20    .PARAMETER NoStore
 21        If specified, the 'no-store' directive will be added to the Cache-Control header.
 22    .PARAMETER MaxAge
 23        If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 24    .PARAMETER SharedMaxAge
 25        If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 26        (used by shared caches).
 27    .PARAMETER MaxStale
 28        If specified, the 'max-stale' directive will be added to the Cache-Control header.
 29    .PARAMETER MaxStaleLimit
 30        If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 31    .PARAMETER MinFresh
 32        If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 33    .PARAMETER NoTransform
 34        If specified, the 'no-transform' directive will be added to the Cache-Control header.
 35    .PARAMETER OnlyIfCached
 36        If specified, the 'only-if-cached' directive will be added to the Cache-Control header.
 37    .PARAMETER Public
 38        If specified, the 'public' directive will be added to the Cache-Control header.
 39    .PARAMETER Private
 40        If specified, the 'private' directive will be added to the Cache-Control header.
 41    .PARAMETER MustRevalidate
 42        If specified, the 'must-revalidate' directive will be added to the Cache-Control header.
 43    .PARAMETER ProxyRevalidate
 44        If specified, the 'proxy-revalidate' directive will be added to the Cache-Control header.
 45    .PARAMETER PassThru
 46        If specified, returns the modified server instance after adding response caching.
 47    .EXAMPLE
 48        $server | Add-KrCacheMiddleware -SizeLimit 10485760 -MaximumBody 65536 -UseCaseSensitivePaths
 49        This example adds response caching to the server with a size limit of 10MB, a maximum body size of 64KB,
 50        and enables case-sensitive paths.
 51    .EXAMPLE
 52        $server | Add-KrCacheMiddleware
 53        This example adds response caching to the server with default settings.
 54    .NOTES
 55        This cmdlet is used to enable and configure response caching for the Kestrun server,
 56#>
 57function Add-KrCacheMiddleware {
 58    [KestrunRuntimeApi('Definition')]
 59    [CmdletBinding()]
 60    [OutputType([Kestrun.Hosting.KestrunHost])]
 61    param(
 62        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 63        [Kestrun.Hosting.KestrunHost]$Server,
 64        [Parameter()]
 65        [long]$SizeLimit,
 66        [Parameter()]
 67        [long]$MaximumBodySize,
 68        [Parameter()]
 69        [switch]$UseCaseSensitivePaths,
 70
 71        [Parameter()]
 72        [switch]$NoCache,
 73        [Parameter()]
 74        [switch]$NoStore,
 75        [Parameter()]
 76        [ValidateRange(1, [int]::MaxValue)]
 77        [int]$MaxAge,
 78        [Parameter()]
 79        [ValidateRange(1, [int]::MaxValue)]
 80        [int]$SharedMaxAge,
 81        [Parameter()]
 82        [switch]$MaxStale,
 83        [Parameter()]
 84        [ValidateRange(1, [int]::MaxValue)]
 85        [int]$MaxStaleLimit,
 86        [Parameter()]
 87        [ValidateRange(1, [int]::MaxValue)]
 88        [int]$MinFresh,
 89        [Parameter()]
 90        [switch]$NoTransform,
 91        [Parameter()]
 92        [switch]$OnlyIfCached,
 93        [Parameter()]
 94        [switch]$Public,
 95        [Parameter()]
 96        [switch]$Private,
 97        [Parameter()]
 98        [switch]$MustRevalidate,
 99        [Parameter()]
 100        [switch]$ProxyRevalidate,
 101        [Parameter()]
 102        [switch]$PassThru
 103    )
 104    begin {
 105        # Ensure the server instance is resolved
 0106        $Server = Resolve-KestrunServer -Server $Server
 107    }
 108    process {
 0109        $options = [Microsoft.AspNetCore.ResponseCaching.ResponseCachingOptions]::new()
 0110        if ($PSBoundParameters.ContainsKey('SizeLimit')) {
 0111            $options.SizeLimit = $SizeLimit
 112        }
 0113        if ($PSBoundParameters.ContainsKey('MaximumBodySize')) {
 0114            $options.MaximumBodySize = $MaximumBodySize
 115        }
 0116        if ($PSBoundParameters.ContainsKey('UseCaseSensitivePaths')) {
 0117            $options.UseCaseSensitivePaths = $UseCaseSensitivePaths.IsPresent
 118        }
 119
 120        # Define default cache control headers if not provided
 0121        $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0122        if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0123        if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0124        if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0125        if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar
 0126        if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0127        if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma
 0128        if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) }
 0129        if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0130        if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0131        if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0132        if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0133        if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent
 0134        if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres
 135
 136        # Add response caching middleware to the server
 0137        [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCaching($Server, $options, $cacheControl) | Out-Nu
 138
 0139        if ($PassThru.IsPresent) {
 140            # if the PassThru switch is specified, return the modified server instance
 0141            return $Server
 142        }
 143    }
 144}
 145

Methods/Properties

Add-KrCacheMiddleware()