PortBlog.API/Shared/KBR.Cache/Extensions/ServiceCollectionExtensions.cs
Bangara Raju Kottedi 3a7edb23c7 Add JWT, OTP, and caching features
Upgraded projects to .NET 9.0 and added new projects `KBR.Cache`, `KBR.Shared`, and `KBR.Shared.Lite` to the solution. Introduced JWT authentication and OTP handling with new models, services, and configuration options. Updated database schema with new entities `Users` and `RefreshTokens`, and added migrations for schema changes. Implemented caching strategies using `AppDistributedCache` with support for in-memory, SQL Server, and Redis. Enhanced email handling with `MailHelpers` for domain replacement. Updated controllers, repositories, and configuration files to support new features.
2025-11-22 06:52:59 +05:30

48 lines
1.7 KiB
C#

using KBR.Cache.Constants;
using KBR.Cache.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Diagnostics.CodeAnalysis;
namespace KBR.Cache.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCache([NotNull] this IServiceCollection services, IConfiguration configuration)
{
// Ensure the 'Microsoft.Extensions.Options.ConfigurationExtensions' package is referenced in your project
var cacheOptions = configuration.GetSection("Cache").Get<CacheOptions>();
if (cacheOptions == null || cacheOptions.Provider == CacheProviders.InMemory)
{
services.AddDistributedMemoryCache();
}
else if (cacheOptions.Provider == CacheProviders.SqlServer)
{
services.AddDistributedMySqlCache(options =>
{
options.ConnectionString = cacheOptions.ConnectionString;
options.TableName = "Cache";
});
}
else if (cacheOptions.Provider == CacheProviders.Redis)
{
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = cacheOptions.ConnectionString;
options.InstanceName = "SampleInstance";
});
}
else
{
throw new Exception("Cache options are not valid");
}
services.TryAddSingleton<IAppDistributedCache, AppDistributedCache>();
return services;
}
}
}