• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vposd / CachedQueries / 4055265175

pending completion
4055265175

Pull #15

github

GitHub
Merge 0046186a7 into 541d6e495
Pull Request #15: Release 1.0.16

2 of 28 branches covered (7.14%)

Branch coverage included in aggregate %.

88 of 88 new or added lines in 7 files covered. (100.0%)

19 of 182 relevant lines covered (10.44%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/CachedQueries.Core/Cache/DistributedCache.cs
1
using System.Text.Json;
2
using System.Text.Json.Serialization;
3
using CachedQueries.Core.Interfaces;
4
using Microsoft.Extensions.Caching.Distributed;
5
using Microsoft.Extensions.Logging;
6

7
namespace CachedQueries.Core.Cache;
8

9
/// <summary>
10
///     Cache service using IDistributedCache implementation.
11
/// </summary>
12
public class DistributedCache : ICacheStore
13
{
14
    private readonly IDistributedCache _cache;
15
    private readonly ILockManager _lockManager;
16
    private readonly CacheOptions _options;
17
    private readonly ILogger<DistributedCache> _logger;
18

19
    private readonly JsonSerializerOptions _settings = new()
×
20
    {
×
21
        ReferenceHandler = ReferenceHandler.Preserve
×
22
    };
×
23

24
    public DistributedCache(IDistributedCache cache, ILoggerFactory loggerFactory, ILockManager lockManager,
×
25
        CacheOptions options)
×
26
    {
×
27
        _cache = cache;
×
28
        _lockManager = lockManager;
×
29
        _options = options;
×
30
        _logger = loggerFactory.CreateLogger<DistributedCache>();
×
31
    }
×
32

33
    public async Task DeleteAsync(string key, bool useLock = true, CancellationToken cancellationToken = default)
34
    {
×
35
        try
36
        {
×
37
            await _cache.RemoveAsync(key, cancellationToken);
×
38
        }
×
39
        catch (Exception exception)
×
40
        {
×
41
            Log(LogLevel.Error, "Error delete cached data: @{Message}", exception.Message);
×
42
        }
×
43
    }
×
44

45
    public async Task<T?> GetAsync<T>(string key, bool useLock = true, CancellationToken cancellationToken = default)
46
    {
×
47
        try
48
        {
×
49
            if (useLock)
×
50
                await _lockManager.CheckLockAsync(key, cancellationToken);
×
51

52
            var cachedResponse = await _cache.GetAsync(key, cancellationToken);
×
53

54
            return cachedResponse is not null
×
55
                ? JsonSerializer.Deserialize<T>(cachedResponse, _settings)
×
56
                : default;
×
57
        }
58
        catch (Exception exception)
×
59
        {
×
60
            Log(LogLevel.Error, "Error loading cached data: @{Message}", exception.Message);
×
61
            return default;
×
62
        }
63
    }
×
64

65
    public async Task SetAsync<T>(string key, T value, bool useLock = true, TimeSpan? expire = null,
66
        CancellationToken cancellationToken = default)
67
    {
×
68
        try
69
        {
×
70
            var response = JsonSerializer.SerializeToUtf8Bytes(value, _settings);
×
71

72
            if (useLock)
×
73
                await _lockManager.LockAsync(key, _options.LockTimeout);
×
74

75
            await _cache.SetAsync(
×
76
                key,
×
77
                response,
×
78
                new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = expire },
×
79
                cancellationToken);
×
80

81
            if (useLock)
×
82
                await _lockManager.ReleaseLockAsync(key);
×
83
        }
×
84
        catch (Exception exception)
×
85
        {
×
86
            Log(LogLevel.Error, "Error setting cached data: @{Message}", exception.Message);
×
87
        }
×
88
    }
×
89

90
    public void Log(LogLevel logLevel, string? message, params object?[] args)
91
    {
×
92
        _logger.Log(logLevel, message, args);
×
93
    }
×
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc