< Summary

Information
Class: Intentum.Core.Caching.MemoryIntentResultCache
Assembly: Intentum.Core
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Core/Caching/MemoryIntentResultCache.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 36
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
TryGetAsync(...)0%660%
SetAsync(...)0%220%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Core/Caching/MemoryIntentResultCache.cs

#LineLine coverage
 1namespace Intentum.Core.Caching;
 2
 3/// <summary>
 4/// In-memory implementation of <see cref="IIntentResultCache"/> for single-node or testing.
 5/// For distributed scenarios use Redis (e.g. Intentum.AI.Caching.Redis or custom implementation).
 6/// </summary>
 7public sealed class MemoryIntentResultCache : IIntentResultCache
 8{
 09    private readonly Dictionary<string, (string Value, DateTimeOffset? ExpiresAt)> _store = new();
 010    private readonly object _lock = new();
 11
 12    /// <inheritdoc />
 13    public Task<(bool Found, string? Value)> TryGetAsync(string key, CancellationToken cancellationToken = default)
 014    {
 015        lock (_lock)
 016        {
 017            if (!_store.TryGetValue(key, out var entry))
 018                return Task.FromResult<(bool Found, string? Value)>((false, null));
 019            if (entry.ExpiresAt.HasValue && DateTimeOffset.UtcNow >= entry.ExpiresAt.Value)
 020            {
 021                _store.Remove(key);
 022                return Task.FromResult<(bool Found, string? Value)>((false, null));
 23            }
 024            return Task.FromResult<(bool Found, string? Value)>((true, entry.Value));
 25        }
 026    }
 27
 28    /// <inheritdoc />
 29    public Task SetAsync(string key, string value, TimeSpan? ttl = null, CancellationToken cancellationToken = default)
 030    {
 031        var expiresAt = ttl.HasValue ? DateTimeOffset.UtcNow + ttl.Value : (DateTimeOffset?)null;
 032        lock (_lock)
 033            _store[key] = (value, expiresAt);
 034        return Task.CompletedTask;
 035    }
 36}