55using tdd_architecture_template_dotnet . Domain . Entities . Products ;
66using tdd_architecture_template_dotnet . Domain . Enums ;
77using tdd_architecture_template_dotnet . Domain . Interfaces . Products ;
8+ using tdd_architecture_template_dotnet . Infrastructure . Singletons . Cache . Interfaces ;
89using tdd_architecture_template_dotnet . Infrastructure . Singletons . Logger . Interfaces ;
910
1011namespace tdd_architecture_template_dotnet . Application . Services . Products
@@ -14,17 +15,20 @@ public class ProductService : IProductService
1415 private readonly IProductRepository _productRepository ;
1516 private readonly IProductTypeRepository _productTypeRepository ;
1617 private readonly ILoggerService _loggerService ;
18+ private readonly ICacheService _cacheService ;
1719 private readonly IMapper _mapper ;
1820
1921 public ProductService (
2022 IProductRepository productRepository ,
2123 IProductTypeRepository productTypeRepository ,
2224 ILoggerService loggerService ,
25+ ICacheService cacheService ,
2326 IMapper mapper )
2427 {
2528 _productRepository = productRepository ;
2629 _productTypeRepository = productTypeRepository ;
2730 _loggerService = loggerService ;
31+ _cacheService = cacheService ;
2832 _mapper = mapper ;
2933 }
3034
@@ -57,6 +61,14 @@ public async Task<Result<ProductViewModel>> GetById(int id)
5761 {
5862 try
5963 {
64+ string cacheKey = $ "Product-{ id } ";
65+
66+ var cachedProduct = _cacheService . Get < Result < ProductViewModel > > ( cacheKey ) ;
67+ if ( cachedProduct != null )
68+ {
69+ return cachedProduct ;
70+ }
71+
6072 var product = await _productRepository . GetById ( id ) ;
6173
6274 if ( product is null )
0 commit comments