나만의 ASP.NET 프레임워크 시작 바로가기
나만의 ASP.NET 프레임워크 (1) 바로가기
나만의 ASP.NET 프레임워크 (2) 바로가기
나만의 ASP.NET 프레임워크 (3) 바로가기
🎯 이번 단계 목표
- 캐싱(Caching) 기능 추가 (메모리, Redis 지원)
- Swagger 자동화 (API 문서 자동 생성)
- JWT 인증/인가 시스템 통합
- 환경 설정(Configuration) 자동화
- 프레임워크 NuGet 배포 준비
🧭 진행 순서
단계 기능 목표
| ✅ 1 | Caching 모듈 | 성능 최적화 (In-Memory, Redis) |
| ✅ 2 | Swagger 자동 구성 | API 문서화 자동화 |
| ✅ 3 | JWT 인증/인가 | 보안, 접근 제어 자동화 |
| ✅ 4 | Configuration 자동화 | 옵션 바인딩, 유지보수성 향상 |
| ✅ 5 | NuGet 패키지화 | 배포 및 재사용성 |
🔥 첫 번째 모듈: Caching 모듈
이제부터 Caching 모듈을 구성해서 프레임워크에서 API 성능을 최적화해보자.
✅ 새 프로젝트 추가: HyochulLab.Caching
- 형식: .NET 8 Class Library
- 참조:
- HyochulLab.Core
- HyochulLab.Core
📁 폴더 구조 예시
HyochulLab.Caching/
├── Extensions/
│ └── ServiceCollectionExtensions.cs
├── Interfaces/
│ └── ICacheService.cs
└── Services/
├── MemoryCacheService.cs
└── RedisCacheService.cs (선택적)
🔧 필요한 NuGet 패키지
In-Memory 캐싱 (필수)
dotnet add package Microsoft.Extensions.Caching.Memory
dotnet add package Microsoft.Extensions.Caching.Memory
Redis 캐싱 (선택)
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
🧩 인터페이스 구성 (ICacheService.cs)
namespace HyochulLab.Caching.Interfaces;
public interface ICacheService
{
Task<T?> GetAsync<T>(string key);
Task SetAsync<T>(string key, T data, TimeSpan cacheDuration);
Task RemoveAsync(string key);
}
namespace HyochulLab.Caching.Interfaces;
public interface ICacheService
{
Task<T?> GetAsync<T>(string key);
Task SetAsync<T>(string key, T data, TimeSpan cacheDuration);
Task RemoveAsync(string key);
}
🧩 메모리 캐시 구현 (MemoryCacheService.cs)
using Microsoft.Extensions.Caching.Memory;
using HyochulLab.Caching.Interfaces;
namespace HyochulLab.Caching.Services;
public class MemoryCacheService : ICacheService
{
private readonly IMemoryCache _cache;
public MemoryCacheService(IMemoryCache cache)
{
_cache = cache;
}
public Task<T?> GetAsync<T>(string key)
{
_cache.TryGetValue(key, out T? data);
return Task.FromResult(data);
}
public Task SetAsync<T>(string key, T data, TimeSpan cacheDuration)
{
_cache.Set(key, data, cacheDuration);
return Task.CompletedTask;
}
public Task RemoveAsync(string key)
{
_cache.Remove(key);
return Task.CompletedTask;
}
}
using Microsoft.Extensions.Caching.Memory;
using HyochulLab.Caching.Interfaces;
namespace HyochulLab.Caching.Services;
public class MemoryCacheService : ICacheService
{
private readonly IMemoryCache _cache;
public MemoryCacheService(IMemoryCache cache)
{
_cache = cache;
}
public Task<T?> GetAsync<T>(string key)
{
_cache.TryGetValue(key, out T? data);
return Task.FromResult(data);
}
public Task SetAsync<T>(string key, T data, TimeSpan cacheDuration)
{
_cache.Set(key, data, cacheDuration);
return Task.CompletedTask;
}
public Task RemoveAsync(string key)
{
_cache.Remove(key);
return Task.CompletedTask;
}
}
🛠 캐싱 모듈 DI 자동 등록 (ServiceCollectionExtensions.cs)
using HyochulLab.Caching.Interfaces;
using HyochulLab.Caching.Services;
using Microsoft.Extensions.DependencyInjection;
namespace HyochulLab.Caching.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddHyochulLabCaching(this IServiceCollection services)
{
services.AddMemoryCache(); // 메모리 캐시 기본 등록
services.AddSingleton<ICacheService, MemoryCacheService>();
// Redis 추가 시:
// services.AddStackExchangeRedisCache(options =>
// {
// options.Configuration = "redis-connection-string";
// });
// services.AddSingleton<ICacheService, RedisCacheService>();
return services;
}
}
using HyochulLab.Caching.Interfaces;
using HyochulLab.Caching.Services;
using Microsoft.Extensions.DependencyInjection;
namespace HyochulLab.Caching.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddHyochulLabCaching(this IServiceCollection services)
{
services.AddMemoryCache(); // 메모리 캐시 기본 등록
services.AddSingleton<ICacheService, MemoryCacheService>();
// Redis 추가 시:
// services.AddStackExchangeRedisCache(options =>
// {
// options.Configuration = "redis-connection-string";
// });
// services.AddSingleton<ICacheService, RedisCacheService>();
return services;
}
}
⚙️ SampleApp에 캐싱 구성 적용 (Program.cs)
using HyochulLab.DependencyInjection.Extensions;
using HyochulLab.Web.Extensions;
using HyochulLab.Caching.Extensions;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Serilog 로깅 구성
builder.Host.UseSerilog((context, configuration) =>
{
configuration.ReadFrom.Configuration(context.Configuration);
});
// HyochulLab 전체 프레임워크 구성
builder.Services.AddHyochulLabFramework(builder.Configuration);
// 캐싱 모듈 추가
builder.Services.AddHyochulLabCaching();
var app = builder.Build();
app.UseHyochulLabExceptionHandling();
app.MapControllers();
app.MapGet("/", () => "✅ HyochulLab 프레임워크 (4) - 캐싱 모듈 적용 완료!");
app.Run();
using HyochulLab.DependencyInjection.Extensions;
using HyochulLab.Web.Extensions;
using HyochulLab.Caching.Extensions;
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Serilog 로깅 구성
builder.Host.UseSerilog((context, configuration) =>
{
configuration.ReadFrom.Configuration(context.Configuration);
});
// HyochulLab 전체 프레임워크 구성
builder.Services.AddHyochulLabFramework(builder.Configuration);
// 캐싱 모듈 추가
builder.Services.AddHyochulLabCaching();
var app = builder.Build();
app.UseHyochulLabExceptionHandling();
app.MapControllers();
app.MapGet("/", () => "✅ HyochulLab 프레임워크 (4) - 캐싱 모듈 적용 완료!");
app.Run();
.png)
댓글
댓글 쓰기