< Summary

Information
Class: TeleFlow.Telegram.Internal.MemoryTelegramChatMemberStatusCache
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/MemoryTelegramChatMemberStatusCache.cs
Line coverage
80%
Covered lines: 17
Uncovered lines: 4
Coverable lines: 21
Total lines: 68
Line coverage: 80.9%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetAsync(...)75%4475%
SetAsync(...)50%4477.77%

File(s)

/_/src/TeleFlow.Framework/Internal/MemoryTelegramChatMemberStatusCache.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using TeleFlow.Annotations;
 3
 4namespace TeleFlow.Telegram.Internal;
 5
 6internal sealed class MemoryTelegramChatMemberStatusCache : ITelegramChatMemberStatusCache
 7{
 98    private readonly ConcurrentDictionary<CacheKey, CacheEntry> _entries = [];
 9    private readonly TimeProvider _timeProvider;
 10
 11    public MemoryTelegramChatMemberStatusCache(TimeProvider timeProvider)
 12    {
 913        ArgumentNullException.ThrowIfNull(timeProvider);
 914        _timeProvider = timeProvider;
 915    }
 16
 17    public ValueTask<TelegramMemberStatusSet?> GetAsync(
 18        long chatId,
 19        long userId,
 20        CancellationToken cancellationToken = default)
 21    {
 1022        cancellationToken.ThrowIfCancellationRequested();
 23
 1024        var key = new CacheKey(chatId, userId);
 25
 1026        if (!_entries.TryGetValue(key, out var entry))
 27        {
 928            return ValueTask.FromResult<TelegramMemberStatusSet?>(null);
 29        }
 30
 131        if (entry.ExpiresAt <= _timeProvider.GetUtcNow())
 32        {
 033            _entries.TryRemove(key, out _);
 034            return ValueTask.FromResult<TelegramMemberStatusSet?>(null);
 35        }
 36
 137        return ValueTask.FromResult<TelegramMemberStatusSet?>(entry.Status);
 38    }
 39
 40    public ValueTask SetAsync(
 41        long chatId,
 42        long userId,
 43        TelegramMemberStatusSet status,
 44        TimeSpan ttl,
 45        CancellationToken cancellationToken = default)
 46    {
 947        cancellationToken.ThrowIfCancellationRequested();
 48
 949        if (!TelegramMemberStatusSetValidator.IsValid(status))
 50        {
 051            throw new ArgumentException("Telegram member status cache value must contain a known status.", nameof(status
 52        }
 53
 954        if (ttl <= TimeSpan.Zero)
 55        {
 056            throw new ArgumentOutOfRangeException(nameof(ttl), "Telegram member status cache TTL must be greater than ze
 57        }
 58
 959        var key = new CacheKey(chatId, userId);
 960        var entry = new CacheEntry(status, _timeProvider.GetUtcNow().Add(ttl));
 961        _entries[key] = entry;
 962        return ValueTask.CompletedTask;
 63    }
 64
 65    private readonly record struct CacheKey(long ChatId, long UserId);
 66
 67    private readonly record struct CacheEntry(TelegramMemberStatusSet Status, DateTimeOffset ExpiresAt);
 68}