| | | 1 | | using Serilog.Events; |
| | | 2 | | using Microsoft.Extensions.Caching.Memory; |
| | | 3 | | using Microsoft.Extensions.Caching.Distributed; |
| | | 4 | | using Microsoft.Extensions.Caching.StackExchangeRedis; |
| | | 5 | | using Microsoft.Extensions.Caching.SqlServer; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | |
| | | 8 | | namespace Kestrun.Hosting; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides extension methods for configuring session state and distributed cache in Kestrun. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class KestrunHostSessionExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Adds session state services and middleware to the application. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param> |
| | | 19 | | /// <param name="cfg">The configuration options for session state.</param> |
| | | 20 | | /// <returns>The updated <see cref="KestrunHost" /> instance.</returns> |
| | | 21 | | public static KestrunHost AddSession(this KestrunHost host, SessionOptions? cfg) |
| | | 22 | | { |
| | | 23 | | // Validate parameters |
| | 2 | 24 | | ArgumentNullException.ThrowIfNull(host); |
| | | 25 | | |
| | 2 | 26 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 27 | | { |
| | 0 | 28 | | host.Logger.Debug("Adding Session with configuration: {@Config}", cfg); |
| | | 29 | | } |
| | | 30 | | // Avoid adding multiple session middlewares |
| | 2 | 31 | | if (host.IsServiceRegistered(typeof(IConfigureOptions<SessionOptions>))) |
| | | 32 | | { |
| | 0 | 33 | | throw new InvalidOperationException("Session services are already registered. Only one session configuration |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // Add the session services |
| | 2 | 37 | | _ = host.AddService(services => |
| | 2 | 38 | | { |
| | 1 | 39 | | _ = (cfg is null) ? |
| | 1 | 40 | | services.AddSession() : |
| | 1 | 41 | | services.AddSession(opts => |
| | 1 | 42 | | { |
| | 0 | 43 | | opts.Cookie = cfg.Cookie; |
| | 0 | 44 | | opts.IdleTimeout = cfg.IdleTimeout; |
| | 0 | 45 | | opts.IOTimeout = cfg.IOTimeout; |
| | 1 | 46 | | }); |
| | 3 | 47 | | }); |
| | | 48 | | |
| | 3 | 49 | | return host.Use(app => app.UseSession()); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Checks if a distributed cache implementation is already registered with the host. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="host">The <see cref="KestrunHost" /> to check.</param> |
| | | 56 | | /// <returns>true if a distributed cache is registered; otherwise, false.</returns> |
| | | 57 | | public static bool IsDistributedCacheRegistered(this KestrunHost host) |
| | | 58 | | { |
| | 5 | 59 | | ArgumentNullException.ThrowIfNull(host); |
| | 5 | 60 | | return host.IsServiceRegistered(typeof(IDistributedCache)); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Adds a default implementation of <see cref="IDistributedCache"/> that stores items in memory |
| | | 65 | | /// to the <see cref="KestrunHost" />. Frameworks that require a distributed cache to work |
| | | 66 | | /// can safely add this dependency as part of their dependency list to ensure that there is at least |
| | | 67 | | /// one implementation available. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param> |
| | | 70 | | /// <param name="cfg">The configuration options for the memory distributed cache.</param> |
| | | 71 | | /// <returns>The <see cref="KestrunHost"/> so that additional calls can be chained.</returns> |
| | | 72 | | public static KestrunHost AddDistributedMemoryCache(this KestrunHost host, MemoryDistributedCacheOptions? cfg) |
| | | 73 | | { |
| | 3 | 74 | | ArgumentNullException.ThrowIfNull(host); |
| | 3 | 75 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 76 | | { |
| | 0 | 77 | | host.Logger.Debug("Adding Distributed Memory Cache with configuration: {@Config}", cfg); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Avoid adding multiple distributed cache implementations |
| | 3 | 81 | | if (IsDistributedCacheRegistered(host)) |
| | | 82 | | { |
| | 0 | 83 | | throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // Add the distributed memory cache service |
| | 3 | 87 | | return host.AddService(services => |
| | 3 | 88 | | { |
| | 3 | 89 | | _ = (cfg is null) ? |
| | 3 | 90 | | services.AddDistributedMemoryCache() : |
| | 3 | 91 | | services.AddDistributedMemoryCache(opts => |
| | 3 | 92 | | { |
| | 0 | 93 | | opts.Clock = cfg.Clock; |
| | 0 | 94 | | opts.CompactionPercentage = cfg.CompactionPercentage; |
| | 0 | 95 | | opts.ExpirationScanFrequency = cfg.ExpirationScanFrequency; |
| | 0 | 96 | | opts.SizeLimit = cfg.SizeLimit; |
| | 0 | 97 | | opts.TrackLinkedCacheEntries = cfg.TrackLinkedCacheEntries; |
| | 0 | 98 | | opts.TrackStatistics = cfg.TrackStatistics; |
| | 3 | 99 | | }); |
| | 6 | 100 | | }); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Adds a StackExchange Redis implementation of <see cref="IDistributedCache"/> to the <see cref="KestrunHost" />. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param> |
| | | 107 | | /// <param name="cfg">The configuration options for the Redis cache.</param> |
| | | 108 | | /// <returns>The updated <see cref="KestrunHost" /> instance.</returns> |
| | | 109 | | public static KestrunHost AddStackExchangeRedisCache(this KestrunHost host, RedisCacheOptions cfg) |
| | | 110 | | { |
| | 1 | 111 | | ArgumentNullException.ThrowIfNull(host); |
| | 1 | 112 | | ArgumentNullException.ThrowIfNull(cfg); |
| | 1 | 113 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 114 | | { |
| | 0 | 115 | | host.Logger.Debug("Adding StackExchange Redis Cache with configuration: {@Config}", cfg); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // Avoid adding multiple distributed cache implementations |
| | 1 | 119 | | if (host.IsDistributedCacheRegistered()) |
| | | 120 | | { |
| | 0 | 121 | | throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Ensure that the ConnectionMultiplexerFactory is set to avoid issues with multiple registrations |
| | 1 | 125 | | return host.AddService(services => |
| | 1 | 126 | | { |
| | 1 | 127 | | _ = services.AddStackExchangeRedisCache(opts => |
| | 1 | 128 | | { |
| | 1 | 129 | | opts.Configuration = cfg.Configuration; |
| | 1 | 130 | | opts.ConfigurationOptions = cfg.ConfigurationOptions; |
| | 1 | 131 | | opts.InstanceName = cfg.InstanceName; |
| | 1 | 132 | | opts.ProfilingSession = cfg.ProfilingSession; |
| | 1 | 133 | | opts.ConnectionMultiplexerFactory = cfg.ConnectionMultiplexerFactory; |
| | 2 | 134 | | }); |
| | 2 | 135 | | }); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Adds a SQL Server implementation of <see cref="IDistributedCache"/> to the <see cref="KestrunHost" />. |
| | | 140 | | /// </summary> |
| | | 141 | | /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param> |
| | | 142 | | /// <param name="cfg">The configuration options for the SQL Server cache.</param> |
| | | 143 | | /// <returns>The updated <see cref="KestrunHost" /> instance.</returns> |
| | | 144 | | public static KestrunHost AddDistributedSqlServerCache(this KestrunHost host, SqlServerCacheOptions cfg) |
| | | 145 | | { |
| | 1 | 146 | | ArgumentNullException.ThrowIfNull(host); |
| | 1 | 147 | | ArgumentNullException.ThrowIfNull(cfg); |
| | 1 | 148 | | if (host.Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 149 | | { |
| | 0 | 150 | | host.Logger.Debug("Adding Distributed SQL Server Cache with configuration: {@Config}", cfg); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // Avoid adding multiple distributed cache implementations |
| | 1 | 154 | | if (host.IsDistributedCacheRegistered()) |
| | | 155 | | { |
| | 0 | 156 | | throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | // Ensure that the ConnectionMultiplexerFactory is set to avoid issues with multiple registrations |
| | 1 | 160 | | return host.AddService(services => |
| | 1 | 161 | | { |
| | 1 | 162 | | _ = services.AddDistributedSqlServerCache(opts => |
| | 1 | 163 | | { |
| | 1 | 164 | | opts.ConnectionString = cfg.ConnectionString; |
| | 1 | 165 | | opts.SchemaName = cfg.SchemaName; |
| | 1 | 166 | | opts.TableName = cfg.TableName; |
| | 1 | 167 | | opts.ExpiredItemsDeletionInterval = cfg.ExpiredItemsDeletionInterval; |
| | 1 | 168 | | opts.DefaultSlidingExpiration = cfg.DefaultSlidingExpiration; |
| | 2 | 169 | | }); |
| | 2 | 170 | | }); |
| | | 171 | | } |
| | | 172 | | } |