| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds StackExchange.Redis distributed cache services to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Configures the Kestrun server to use StackExchange.Redis as the distributed cache for session state. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to configure. If not specified, the current server instance is used. |
| | | 8 | | .PARAMETER Options |
| | | 9 | | The Redis cache options to configure. If not specified, default options are used. |
| | | 10 | | .PARAMETER ConfigurationOptions |
| | | 11 | | The StackExchange.Redis configuration options to use. If not specified, the default configuration is used. |
| | | 12 | | This parameter is mutually exclusive with the Configuration parameter. |
| | | 13 | | .PARAMETER Configuration |
| | | 14 | | The configuration string to use for connecting to the Redis server. If not specified, the default configuration |
| | | 15 | | This parameter is mutually exclusive with the ConfigurationOptions parameter. |
| | | 16 | | .PARAMETER InstanceName |
| | | 17 | | The instance name to use for the Redis cache. If not specified, the default instance name is used. |
| | | 18 | | .PARAMETER PassThru |
| | | 19 | | If specified, the cmdlet returns the modified server instance after configuration. |
| | | 20 | | .EXAMPLE |
| | | 21 | | Add-KrDistributedRedisCache -Server $myServer -Options $myRedisOptions |
| | | 22 | | Adds StackExchange.Redis distributed cache services to the specified Kestrun server with the provided options. |
| | | 23 | | .EXAMPLE |
| | | 24 | | Add-KrDistributedRedisCache -Configuration "localhost:6379" -InstanceName "MyApp" |
| | | 25 | | Configures StackExchange.Redis distributed cache with the specified configuration string and instance name. |
| | | 26 | | .EXAMPLE |
| | | 27 | | $configOptions = [StackExchange.Redis.ConfigurationOptions]::Parse("localhost:6379") |
| | | 28 | | $configOptions.AbortOnConnectFail = $false |
| | | 29 | | Add-KrDistributedRedisCache -ConfigurationOptions $configOptions |
| | | 30 | | Configures StackExchange.Redis distributed cache with the specified configuration options. |
| | | 31 | | .NOTES |
| | | 32 | | This cmdlet is part of the Kestrun PowerShell module and is used to configure StackExchange.Redis distributed ca |
| | | 33 | | .LINK |
| | | 34 | | https://docs.kestrun.dev/docs/powershell/kestrun/middleware |
| | | 35 | | #> |
| | | 36 | | function Add-KrDistributedRedisCache { |
| | | 37 | | [KestrunRuntimeApi('Definition')] |
| | | 38 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 39 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 40 | | param( |
| | | 41 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 42 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 43 | | |
| | | 44 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 45 | | [Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions]$Options, |
| | | 46 | | |
| | | 47 | | [Parameter(ParameterSetName = 'Items')] |
| | | 48 | | [StackExchange.Redis.ConfigurationOptions]$ConfigurationOptions, |
| | | 49 | | |
| | | 50 | | [Parameter(ParameterSetName = 'Items')] |
| | | 51 | | [string]$Configuration, |
| | | 52 | | |
| | | 53 | | [Parameter(ParameterSetName = 'Items')] |
| | | 54 | | [string]$InstanceName, |
| | | 55 | | |
| | | 56 | | [Parameter()] |
| | | 57 | | [switch]$PassThru |
| | | 58 | | ) |
| | | 59 | | begin { |
| | | 60 | | # Ensure the server instance is resolved |
| | 0 | 61 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 62 | | } |
| | | 63 | | process { |
| | 0 | 64 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 65 | | # Build Redis cache options from individual parameters |
| | 0 | 66 | | $Options = [Microsoft.Extensions.Caching.StackExchangeRedis.RedisCacheOptions]::new() |
| | | 67 | | |
| | 0 | 68 | | if ($PsBoundParameters.ContainsKey('ConfigurationOptions')) { |
| | 0 | 69 | | $Options.ConfigurationOptions = $ConfigurationOptions |
| | | 70 | | } |
| | 0 | 71 | | if ($PsBoundParameters.ContainsKey('Configuration')) { |
| | 0 | 72 | | $Options.Configuration = $Configuration |
| | | 73 | | } |
| | 0 | 74 | | if ($PsBoundParameters.ContainsKey('InstanceName')) { |
| | 0 | 75 | | $Options.InstanceName = $InstanceName |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | # Add StackExchange.Redis distributed cache to the Kestrun server |
| | 0 | 79 | | [Kestrun.Hosting.KestrunHostSessionExtensions]::AddStackExchangeRedisCache($Server, $Options) | Out-Null |
| | | 80 | | |
| | 0 | 81 | | if ($PassThru.IsPresent) { |
| | | 82 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 83 | | return $Server |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |