sprints work
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
|
||||
<RootNamespace>StellaOps.Provcache.Valkey</RootNamespace>
|
||||
<AssemblyName>StellaOps.Provcache.Valkey</AssemblyName>
|
||||
<Description>Valkey/Redis cache store implementation for StellaOps Provcache</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.8.37" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../StellaOps.Provcache/StellaOps.Provcache.csproj" />
|
||||
<ProjectReference Include="../StellaOps.Messaging.Transport.Valkey/StellaOps.Messaging.Transport.Valkey.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,327 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace StellaOps.Provcache.Valkey;
|
||||
|
||||
/// <summary>
|
||||
/// Valkey/Redis implementation of <see cref="IProvcacheStore"/> with read-through caching.
|
||||
/// </summary>
|
||||
public sealed class ValkeyProvcacheStore : IProvcacheStore, IAsyncDisposable
|
||||
{
|
||||
private readonly IConnectionMultiplexer _connectionMultiplexer;
|
||||
private readonly ProvcacheOptions _options;
|
||||
private readonly ILogger<ValkeyProvcacheStore> _logger;
|
||||
private readonly JsonSerializerOptions _jsonOptions;
|
||||
private readonly SemaphoreSlim _connectionLock = new(1, 1);
|
||||
private IDatabase? _database;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string ProviderName => "valkey";
|
||||
|
||||
public ValkeyProvcacheStore(
|
||||
IConnectionMultiplexer connectionMultiplexer,
|
||||
IOptions<ProvcacheOptions> options,
|
||||
ILogger<ValkeyProvcacheStore> logger)
|
||||
{
|
||||
_connectionMultiplexer = connectionMultiplexer ?? throw new ArgumentNullException(nameof(connectionMultiplexer));
|
||||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
_jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<ProvcacheLookupResult> GetAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var redisKey = BuildKey(veriKey);
|
||||
|
||||
var value = await db.StringGetAsync(redisKey).ConfigureAwait(false);
|
||||
sw.Stop();
|
||||
|
||||
if (value.IsNullOrEmpty)
|
||||
{
|
||||
_logger.LogDebug("Cache miss for VeriKey {VeriKey} in {ElapsedMs}ms", veriKey, sw.Elapsed.TotalMilliseconds);
|
||||
return ProvcacheLookupResult.Miss(sw.Elapsed.TotalMilliseconds);
|
||||
}
|
||||
|
||||
var entry = JsonSerializer.Deserialize<ProvcacheEntry>((string)value!, _jsonOptions);
|
||||
if (entry is null)
|
||||
{
|
||||
_logger.LogWarning("Failed to deserialize cache entry for VeriKey {VeriKey}", veriKey);
|
||||
return ProvcacheLookupResult.Miss(sw.Elapsed.TotalMilliseconds);
|
||||
}
|
||||
|
||||
// Optionally refresh TTL on read (sliding expiration)
|
||||
if (_options.SlidingExpiration)
|
||||
{
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
if (ttl > TimeSpan.Zero)
|
||||
{
|
||||
await db.KeyExpireAsync(redisKey, ttl).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug("Cache hit for VeriKey {VeriKey} in {ElapsedMs}ms", veriKey, sw.Elapsed.TotalMilliseconds);
|
||||
return ProvcacheLookupResult.Hit(entry, ProviderName, sw.Elapsed.TotalMilliseconds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
sw.Stop();
|
||||
_logger.LogError(ex, "Error getting cache entry for VeriKey {VeriKey}", veriKey);
|
||||
return ProvcacheLookupResult.Miss(sw.Elapsed.TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<ProvcacheBatchLookupResult> GetManyAsync(
|
||||
IEnumerable<string> veriKeys,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var keyList = veriKeys.ToList();
|
||||
|
||||
if (keyList.Count == 0)
|
||||
{
|
||||
return new ProvcacheBatchLookupResult
|
||||
{
|
||||
Hits = new Dictionary<string, ProvcacheEntry>(),
|
||||
Misses = [],
|
||||
ElapsedMs = 0
|
||||
};
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var redisKeys = keyList.Select(k => (RedisKey)BuildKey(k)).ToArray();
|
||||
|
||||
var values = await db.StringGetAsync(redisKeys).ConfigureAwait(false);
|
||||
sw.Stop();
|
||||
|
||||
var hits = new Dictionary<string, ProvcacheEntry>();
|
||||
var misses = new List<string>();
|
||||
|
||||
for (int i = 0; i < keyList.Count; i++)
|
||||
{
|
||||
var veriKey = keyList[i];
|
||||
var value = values[i];
|
||||
|
||||
if (value.IsNullOrEmpty)
|
||||
{
|
||||
misses.Add(veriKey);
|
||||
continue;
|
||||
}
|
||||
|
||||
var entry = JsonSerializer.Deserialize<ProvcacheEntry>((string)value!, _jsonOptions);
|
||||
if (entry is not null)
|
||||
{
|
||||
hits[veriKey] = entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
misses.Add(veriKey);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogDebug(
|
||||
"Batch lookup: {Hits} hits, {Misses} misses in {ElapsedMs}ms",
|
||||
hits.Count,
|
||||
misses.Count,
|
||||
sw.Elapsed.TotalMilliseconds);
|
||||
|
||||
return new ProvcacheBatchLookupResult
|
||||
{
|
||||
Hits = hits,
|
||||
Misses = misses,
|
||||
ElapsedMs = sw.Elapsed.TotalMilliseconds
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
sw.Stop();
|
||||
_logger.LogError(ex, "Error in batch cache lookup");
|
||||
return new ProvcacheBatchLookupResult
|
||||
{
|
||||
Hits = new Dictionary<string, ProvcacheEntry>(),
|
||||
Misses = keyList,
|
||||
ElapsedMs = sw.Elapsed.TotalMilliseconds
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask SetAsync(ProvcacheEntry entry, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var redisKey = BuildKey(entry.VeriKey);
|
||||
var value = JsonSerializer.Serialize(entry, _jsonOptions);
|
||||
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
if (ttl <= TimeSpan.Zero)
|
||||
{
|
||||
_logger.LogDebug("Skipping expired entry for VeriKey {VeriKey}", entry.VeriKey);
|
||||
return;
|
||||
}
|
||||
|
||||
// Cap TTL at MaxTtl
|
||||
if (ttl > _options.MaxTtl)
|
||||
{
|
||||
ttl = _options.MaxTtl;
|
||||
}
|
||||
|
||||
await db.StringSetAsync(redisKey, value, ttl).ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Stored cache entry for VeriKey {VeriKey} with TTL {Ttl}", entry.VeriKey, ttl);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error storing cache entry for VeriKey {VeriKey}", entry.VeriKey);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask SetManyAsync(IEnumerable<ProvcacheEntry> entries, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entryList = entries.ToList();
|
||||
if (entryList.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var batch = db.CreateBatch();
|
||||
var tasks = new List<Task>();
|
||||
|
||||
foreach (var entry in entryList)
|
||||
{
|
||||
var redisKey = BuildKey(entry.VeriKey);
|
||||
var value = JsonSerializer.Serialize(entry, _jsonOptions);
|
||||
|
||||
var ttl = entry.ExpiresAt - DateTimeOffset.UtcNow;
|
||||
if (ttl <= TimeSpan.Zero)
|
||||
continue;
|
||||
|
||||
if (ttl > _options.MaxTtl)
|
||||
ttl = _options.MaxTtl;
|
||||
|
||||
tasks.Add(batch.StringSetAsync(redisKey, value, ttl));
|
||||
}
|
||||
|
||||
batch.Execute();
|
||||
await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Batch stored {Count} cache entries", entryList.Count);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in batch cache store");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<bool> InvalidateAsync(string veriKey, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var redisKey = BuildKey(veriKey);
|
||||
|
||||
var deleted = await db.KeyDeleteAsync(redisKey).ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Invalidated cache entry for VeriKey {VeriKey}: {Deleted}", veriKey, deleted);
|
||||
return deleted;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error invalidating cache entry for VeriKey {VeriKey}", veriKey);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<long> InvalidateByPatternAsync(string pattern, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = await GetDatabaseAsync().ConfigureAwait(false);
|
||||
var server = _connectionMultiplexer.GetServer(_connectionMultiplexer.GetEndPoints().First());
|
||||
|
||||
var fullPattern = $"{_options.ValkeyKeyPrefix}{pattern}";
|
||||
var keys = server.Keys(pattern: fullPattern).ToArray();
|
||||
|
||||
if (keys.Length == 0)
|
||||
return 0;
|
||||
|
||||
var deleted = await db.KeyDeleteAsync(keys).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation("Invalidated {Count} cache entries matching pattern {Pattern}", deleted, pattern);
|
||||
return deleted;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error invalidating cache entries by pattern {Pattern}", pattern);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<ProvcacheEntry> GetOrSetAsync(
|
||||
string veriKey,
|
||||
Func<CancellationToken, ValueTask<ProvcacheEntry>> factory,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await GetAsync(veriKey, cancellationToken).ConfigureAwait(false);
|
||||
if (result.IsHit && result.Entry is not null)
|
||||
{
|
||||
return result.Entry;
|
||||
}
|
||||
|
||||
var entry = await factory(cancellationToken).ConfigureAwait(false);
|
||||
await SetAsync(entry, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
private string BuildKey(string veriKey) => $"{_options.ValkeyKeyPrefix}{veriKey}";
|
||||
|
||||
private async Task<IDatabase> GetDatabaseAsync()
|
||||
{
|
||||
if (_database is not null)
|
||||
return _database;
|
||||
|
||||
await _connectionLock.WaitAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
_database ??= _connectionMultiplexer.GetDatabase();
|
||||
return _database;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_connectionLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
_connectionLock.Dispose();
|
||||
|
||||
// Note: Don't dispose the connection multiplexer if it's shared (injected via DI)
|
||||
// The DI container will handle its lifetime
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user