< Summary

Information
Class: c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Infrastructure\Caching\CacheService.cs
Assembly: Default
File(s): c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Infrastructure\Caching\CacheService.cs
Line coverage
100%
Covered lines: 16
Uncovered lines: 0
Coverable lines: 16
Total lines: 35
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

c:\Source 2025\CurrencyConverter\CurrencyConverter\src\Infrastructure\Caching\CacheService.cs

#LineLine coverage
 1using Application.Abstractions;
 2using Microsoft.Extensions.Caching.Distributed;
 3using Newtonsoft.Json;
 4
 5namespace Infrastructure.Caching
 6{
 7    internal class CacheService : ICacheService
 8    {
 9
 10        private readonly IDistributedCache _cache;
 711        public CacheService(IDistributedCache cache)
 712        {
 713            _cache = cache;
 714        }
 15
 16        public async Task<T?> GetAsync<T>(string key, CancellationToken cancellationToken)
 17               where T : class
 518        {
 519            string? cachedValue = await _cache.GetStringAsync(key, cancellationToken);
 520            if (cachedValue == null)
 121            {
 122                return null;
 23            }
 424            T? value = JsonConvert.DeserializeObject<T>(cachedValue);
 425            return value;
 526        }
 27
 28        public async Task SetAsync<T>(string key, T value, CancellationToken cancellationToken = default)
 29            where T : class
 130        {
 131            string cacheValue = JsonConvert.SerializeObject(value);
 132            await _cache.SetStringAsync(key, cacheValue, cancellationToken);
 133        }
 34    }
 35}