58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using StellaOps.Messaging.Abstractions;
|
|
|
|
namespace StellaOps.Messaging.Transport.Valkey;
|
|
|
|
/// <summary>
|
|
/// Factory for creating Valkey distributed cache instances.
|
|
/// </summary>
|
|
public sealed class ValkeyCacheFactory : IDistributedCacheFactory
|
|
{
|
|
private readonly ValkeyConnectionFactory _connectionFactory;
|
|
private readonly ILoggerFactory? _loggerFactory;
|
|
private readonly JsonSerializerOptions _jsonOptions;
|
|
|
|
public ValkeyCacheFactory(
|
|
ValkeyConnectionFactory connectionFactory,
|
|
ILoggerFactory? loggerFactory = null,
|
|
JsonSerializerOptions? jsonOptions = null)
|
|
{
|
|
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
|
|
_loggerFactory = loggerFactory;
|
|
_jsonOptions = jsonOptions ?? new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
WriteIndented = false
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string ProviderName => "valkey";
|
|
|
|
/// <inheritdoc />
|
|
public IDistributedCache<TKey, TValue> Create<TKey, TValue>(CacheOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
|
|
return new ValkeyCacheStore<TKey, TValue>(
|
|
_connectionFactory,
|
|
options,
|
|
_loggerFactory?.CreateLogger<ValkeyCacheStore<TKey, TValue>>(),
|
|
_jsonOptions);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IDistributedCache<TValue> Create<TValue>(CacheOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
|
|
return new ValkeyCacheStore<TValue>(
|
|
_connectionFactory,
|
|
options,
|
|
_loggerFactory?.CreateLogger<ValkeyCacheStore<TValue>>(),
|
|
_jsonOptions);
|
|
}
|
|
}
|