| | | 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 | | #> |
| | | 57 | | function 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 |
| | 0 | 106 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 107 | | } |
| | | 108 | | process { |
| | 0 | 109 | | $options = [Microsoft.AspNetCore.ResponseCaching.ResponseCachingOptions]::new() |
| | 0 | 110 | | if ($PSBoundParameters.ContainsKey('SizeLimit')) { |
| | 0 | 111 | | $options.SizeLimit = $SizeLimit |
| | | 112 | | } |
| | 0 | 113 | | if ($PSBoundParameters.ContainsKey('MaximumBodySize')) { |
| | 0 | 114 | | $options.MaximumBodySize = $MaximumBodySize |
| | | 115 | | } |
| | 0 | 116 | | if ($PSBoundParameters.ContainsKey('UseCaseSensitivePaths')) { |
| | 0 | 117 | | $options.UseCaseSensitivePaths = $UseCaseSensitivePaths.IsPresent |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | # Define default cache control headers if not provided |
| | 0 | 121 | | $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new(); |
| | 0 | 122 | | if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent } |
| | 0 | 123 | | if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent } |
| | 0 | 124 | | if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) } |
| | 0 | 125 | | if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar |
| | 0 | 126 | | if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent } |
| | 0 | 127 | | if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma |
| | 0 | 128 | | if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) } |
| | 0 | 129 | | if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent } |
| | 0 | 130 | | if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent } |
| | 0 | 131 | | if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent } |
| | 0 | 132 | | if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent } |
| | 0 | 133 | | if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent |
| | 0 | 134 | | if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres |
| | | 135 | | |
| | | 136 | | # Add response caching middleware to the server |
| | 0 | 137 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCaching($Server, $options, $cacheControl) | Out-Nu |
| | | 138 | | |
| | 0 | 139 | | if ($PassThru.IsPresent) { |
| | | 140 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 141 | | return $Server |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | |