< 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@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 127
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 12/01/2025 - 20:55:19 Line coverage: 0% (0/25) Total lines: 145 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa05/09/2026 - 21:51:36 Line coverage: 0% (0/23) Total lines: 127 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

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 SizeLimit
 8        The maximum size, in bytes, of the response cache. If not specified, the default
 9        size limit of the underlying implementation will be used.
 10    .PARAMETER MaximumBodySize
 11        The maximum size, in bytes, of the response body that can be cached. If not specified,
 12        the default value of 64KB will be used.
 13    .PARAMETER UseCaseSensitivePaths
 14        If specified, the caching will be case-sensitive with respect to request paths.
 15    .PARAMETER NoCache
 16        If specified, the 'no-cache' directive will be added to the Cache-Control header.
 17    .PARAMETER NoStore
 18        If specified, the 'no-store' directive will be added to the Cache-Control header.
 19    .PARAMETER MaxAge
 20        If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 21    .PARAMETER SharedMaxAge
 22        If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 23        (used by shared caches).
 24    .PARAMETER MaxStale
 25        If specified, the 'max-stale' directive will be added to the Cache-Control header.
 26    .PARAMETER MaxStaleLimit
 27        If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 28    .PARAMETER MinFresh
 29        If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 30    .PARAMETER NoTransform
 31        If specified, the 'no-transform' directive will be added to the Cache-Control header.
 32    .PARAMETER OnlyIfCached
 33        If specified, the 'only-if-cached' directive will be added to the Cache-Control header.
 34    .PARAMETER Public
 35        If specified, the 'public' directive will be added to the Cache-Control header.
 36    .PARAMETER Private
 37        If specified, the 'private' directive will be added to the Cache-Control header.
 38    .PARAMETER MustRevalidate
 39        If specified, the 'must-revalidate' directive will be added to the Cache-Control header.
 40    .PARAMETER ProxyRevalidate
 41        If specified, the 'proxy-revalidate' directive will be added to the Cache-Control header.
 42    .EXAMPLE
 43        Add-KrCacheMiddleware -SizeLimit 10485760 -MaximumBody 65536 -UseCaseSensitivePaths
 44        This example adds response caching to the server with a size limit of 10MB, a maximum body size of 64KB,
 45        and enables case-sensitive paths.
 46    .EXAMPLE
 47        Add-KrCacheMiddleware
 48        This example adds response caching to the server with default settings.
 49    .NOTES
 50        This cmdlet is used to enable and configure response caching for the Kestrun server,
 51#>
 52function Add-KrCacheMiddleware {
 53    [KestrunRuntimeApi('Definition')]
 54    [CmdletBinding()]
 55    param(
 56        [Parameter()]
 57        [long]$SizeLimit,
 58        [Parameter()]
 59        [long]$MaximumBodySize,
 60        [Parameter()]
 61        [switch]$UseCaseSensitivePaths,
 62
 63        [Parameter()]
 64        [switch]$NoCache,
 65        [Parameter()]
 66        [switch]$NoStore,
 67        [Parameter()]
 68        [ValidateRange(1, [int]::MaxValue)]
 69        [int]$MaxAge,
 70        [Parameter()]
 71        [ValidateRange(1, [int]::MaxValue)]
 72        [int]$SharedMaxAge,
 73        [Parameter()]
 74        [switch]$MaxStale,
 75        [Parameter()]
 76        [ValidateRange(1, [int]::MaxValue)]
 77        [int]$MaxStaleLimit,
 78        [Parameter()]
 79        [ValidateRange(1, [int]::MaxValue)]
 80        [int]$MinFresh,
 81        [Parameter()]
 82        [switch]$NoTransform,
 83        [Parameter()]
 84        [switch]$OnlyIfCached,
 85        [Parameter()]
 86        [switch]$Public,
 87        [Parameter()]
 88        [switch]$Private,
 89        [Parameter()]
 90        [switch]$MustRevalidate,
 91        [Parameter()]
 92        [switch]$ProxyRevalidate
 93    )
 94    # Ensure the server instance is resolved
 095    $Server = Resolve-KestrunServer
 96
 097    $options = [Microsoft.AspNetCore.ResponseCaching.ResponseCachingOptions]::new()
 098    if ($PSBoundParameters.ContainsKey('SizeLimit')) {
 099        $options.SizeLimit = $SizeLimit
 100    }
 0101    if ($PSBoundParameters.ContainsKey('MaximumBodySize')) {
 0102        $options.MaximumBodySize = $MaximumBodySize
 103    }
 0104    if ($PSBoundParameters.ContainsKey('UseCaseSensitivePaths')) {
 0105        $options.UseCaseSensitivePaths = $UseCaseSensitivePaths.IsPresent
 106    }
 107
 108    # Define default cache control headers if not provided
 0109    $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0110    if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0111    if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0112    if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0113    if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($SharedMa
 0114    if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0115    if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($MaxSta
 0116    if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) }
 0117    if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0118    if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0119    if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0120    if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0121    if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent }
 0122    if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPresent 
 123
 124    # Add response caching middleware to the server
 0125    [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCaching($Server, $options, $cacheControl) | Out-Null
 126}
 127

Methods/Properties

Add-KrCacheMiddleware()