< Summary - Kestrun — Combined Coverage

Information
Class: Public.Session.Add-KrDistributedSqlServerCache
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Session/Add-KrDistributedSqlServerCache.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 101
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 10/13/2025 - 16:52:37 Line coverage: 0% (0/16) Total lines: 101 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Session/Add-KrDistributedSqlServerCache.ps1

#LineLine coverage
 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 #>
 36function 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
 069        $Server = Resolve-KestrunServer -Server $Server
 70    }
 71    process {
 072        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 73            # Build SQL Server distributed cache options
 074            $Options = [Microsoft.Extensions.Caching.SqlServer.SqlServerCacheOptions]::new()
 75
 076            if ($PsBoundParameters.ContainsKey('ConnectionString')) {
 077                $Options.ConnectionString = $ConnectionString
 78            }
 079            if ($PsBoundParameters.ContainsKey('SchemaName')) {
 080                $Options.SchemaName = $SchemaName
 81            }
 082            if ($PsBoundParameters.ContainsKey('TableName')) {
 083                $Options.TableName = $TableName
 84            }
 085            if ($PsBoundParameters.ContainsKey('ExpiredItemsDeletionInterval')) {
 086                $Options.ExpiredItemsDeletionInterval = [TimeSpan]::FromSeconds($ExpiredItemsDeletionInterval)
 87            }
 088            if ($PsBoundParameters.ContainsKey('DefaultSlidingExpiration')) {
 089                $Options.DefaultSlidingExpiration = [TimeSpan]::FromSeconds($DefaultSlidingExpiration)
 90            }
 91        }
 92
 93        # Register the SQL Server distributed cache with the host
 094        [Kestrun.Hosting.KestrunHostSessionExtensions]::AddDistributedSqlServerCache($Server, $Options) | Out-Null
 95
 096        if ($PassThru.IsPresent) {
 97            # if the PassThru switch is specified, return the modified server instance
 098            return $Server
 99        }
 100    }
 101}