| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds SQL Server distributed cache services to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Configures the Kestrun server to use SQL Server 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 SQL Server cache options to configure. If not specified, default options are used. |
| | | 10 | | .PARAMETER ConnectionString |
| | | 11 | | The connection string to the SQL Server database. If not specified, the default connection string is used. |
| | | 12 | | .PARAMETER SchemaName |
| | | 13 | | The schema name to use for the SQL Server cache. If not specified, the default schema name is used. |
| | | 14 | | .PARAMETER TableName |
| | | 15 | | The table name to use for the SQL Server cache. If not specified, the default table name is used. |
| | | 16 | | .PARAMETER ExpiredItemsDeletionInterval |
| | | 17 | | The interval in seconds at which expired items will be deleted from the cache. If not specified, the default is |
| | | 18 | | .PARAMETER DefaultSlidingExpiration |
| | | 19 | | The default sliding expiration in seconds for cache items. If not specified, the default is 20 minutes. |
| | | 20 | | .PARAMETER PassThru |
| | | 21 | | If specified, the cmdlet returns the modified server instance after configuration. |
| | | 22 | | .EXAMPLE |
| | | 23 | | Add-KrDistributedSqlServerCache -Server $myServer -Options $mySqlOptions |
| | | 24 | | Adds SQL Server distributed cache services to the specified Kestrun server with the provided options. |
| | | 25 | | .EXAMPLE |
| | | 26 | | Add-KrDistributedSqlServerCache -ConnectionString "Server=myServer;Database=myDB;User Id=myUser;Password=myPass; |
| | | 27 | | Configures SQL Server distributed cache with the specified connection string, schema name, and table name. |
| | | 28 | | .EXAMPLE |
| | | 29 | | Add-KrDistributedSqlServerCache -ExpiredItemsDeletionInterval 1800 -DefaultSlidingExpiration 1200 |
| | | 30 | | Configures SQL Server distributed cache with an expired items deletion interval of 1800 seconds and a default sl |
| | | 31 | | .NOTES |
| | | 32 | | This cmdlet is part of the Kestrun PowerShell module and is used to configure SQL Server distributed cache for K |
| | | 33 | | .LINK |
| | | 34 | | https://docs.kestrun.dev/docs/powershell/kestrun/middleware |
| | | 35 | | #> |
| | | 36 | | function Add-KrDistributedSqlServerCache { |
| | | 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.SqlServer.SqlServerCacheOptions]$Options, |
| | | 46 | | |
| | | 47 | | [Parameter(ParameterSetName = 'Items')] |
| | | 48 | | [string]$ConnectionString, |
| | | 49 | | |
| | | 50 | | [Parameter(ParameterSetName = 'Items')] |
| | | 51 | | [string]$SchemaName, |
| | | 52 | | |
| | | 53 | | [Parameter(ParameterSetName = 'Items')] |
| | | 54 | | [string]$TableName, |
| | | 55 | | |
| | | 56 | | [Parameter(ParameterSetName = 'Items')] |
| | | 57 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 58 | | [int]$ExpiredItemsDeletionInterval, |
| | | 59 | | |
| | | 60 | | [Parameter(ParameterSetName = 'Items')] |
| | | 61 | | [ValidateRange(1, [int]::MaxValue)] |
| | | 62 | | [int]$DefaultSlidingExpiration, |
| | | 63 | | |
| | | 64 | | [Parameter()] |
| | | 65 | | [switch]$PassThru |
| | | 66 | | ) |
| | | 67 | | begin { |
| | | 68 | | # Ensure the server instance is resolved |
| | 0 | 69 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 70 | | } |
| | | 71 | | process { |
| | 0 | 72 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | | 73 | | # Build SQL Server distributed cache options |
| | 0 | 74 | | $Options = [Microsoft.Extensions.Caching.SqlServer.SqlServerCacheOptions]::new() |
| | | 75 | | |
| | 0 | 76 | | if ($PsBoundParameters.ContainsKey('ConnectionString')) { |
| | 0 | 77 | | $Options.ConnectionString = $ConnectionString |
| | | 78 | | } |
| | 0 | 79 | | if ($PsBoundParameters.ContainsKey('SchemaName')) { |
| | 0 | 80 | | $Options.SchemaName = $SchemaName |
| | | 81 | | } |
| | 0 | 82 | | if ($PsBoundParameters.ContainsKey('TableName')) { |
| | 0 | 83 | | $Options.TableName = $TableName |
| | | 84 | | } |
| | 0 | 85 | | if ($PsBoundParameters.ContainsKey('ExpiredItemsDeletionInterval')) { |
| | 0 | 86 | | $Options.ExpiredItemsDeletionInterval = [TimeSpan]::FromSeconds($ExpiredItemsDeletionInterval) |
| | | 87 | | } |
| | 0 | 88 | | if ($PsBoundParameters.ContainsKey('DefaultSlidingExpiration')) { |
| | 0 | 89 | | $Options.DefaultSlidingExpiration = [TimeSpan]::FromSeconds($DefaultSlidingExpiration) |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | # Register the SQL Server distributed cache with the host |
| | 0 | 94 | | [Kestrun.Hosting.KestrunHostSessionExtensions]::AddDistributedSqlServerCache($Server, $Options) | Out-Null |
| | | 95 | | |
| | 0 | 96 | | if ($PassThru.IsPresent) { |
| | | 97 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 98 | | return $Server |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |