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

vposd / CachedQueries / 4055301632

pending completion
4055301632

Pull #16

github

GitHub
Merge 0046186a7 into 541d6e495
Pull Request #16: 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/MemoryCache.cs
1
using System.Text.Json;
2
using System.Text.Json.Serialization;
3
using CachedQueries.Core.Interfaces;
4
using Microsoft.Extensions.Caching.Memory;
5
using Microsoft.Extensions.Logging;
6

7
namespace CachedQueries.Core.Cache;
8

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

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

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

32
    public async Task<T?> GetAsync<T>(string key, bool useLock = true, CancellationToken cancellationToken = default)
33
    {
×
34
        try
35
        {
×
36
            if (useLock)
×
37
                await _lockManager.CheckLockAsync(key, cancellationToken);
×
38

39
            _cache.TryGetValue(key, out var value);
×
40
            var cachedResponse = value?.ToString();
×
41

42
            var result = cachedResponse is not null
×
43
                ? JsonSerializer.Deserialize<T>(cachedResponse, _settings)
×
44
                : default;
×
45
            return result;
×
46
        }
47
        catch (Exception exception)
×
48
        {
×
49
            Log(LogLevel.Error, "Error loading cached data: @{Message}", exception.Message);
×
50
            return default;
×
51
        }
52
    }
×
53

54
    public async Task SetAsync<T>(string key, T value, bool useLock = true, TimeSpan? expire = null,
55
        CancellationToken cancellationToken = default)
56
    {
×
57
        try
58
        {
×
59
            var serialized = JsonSerializer.Serialize(value, _settings);
×
60

61
            if (useLock)
×
62
                await _lockManager.LockAsync(key, _options.LockTimeout);
×
63

64
            _cache.Set(key, serialized, new MemoryCacheEntryOptions { SlidingExpiration = expire });
×
65

66
            if (useLock)
×
67
                await _lockManager.ReleaseLockAsync(key);
×
68
        }
×
69
        catch (Exception exception)
×
70
        {
×
71
            Log(LogLevel.Error, "Error setting cached data: @{Message}", exception.Message);
×
72
        }
×
73
    }
×
74

75
    public async Task DeleteAsync(string key, bool useLock = true, CancellationToken cancellationToken = default)
76
    {
×
77
        try
78
        {
×
79
            _cache.Remove(key);
×
80
        }
×
81
        catch (Exception exception)
×
82
        {
×
83
            Log(LogLevel.Error, "Error delete cached data: @{Message}", exception.Message);
×
84
        }
×
85
    }
×
86

87
    public void Log(LogLevel logLevel, string? message, params object?[] args)
88
    {
×
89
        _logger.Log(logLevel, message, args);
×
90
    }
×
91
}
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