mirror of
https://github.com/hanzoai/jube-fork.git
synced 2026-10-30 18:33:33 +00:00
* Replaced the StackOverflow Redis client in the Cache library with the Resilient Redis Library. * Improved policy wrappers by introducing two distinct policies: one for idempotent operations and one for increment operations. Added a circuit breaker to the Redis Resiliency layer to prevent thundering herd on re-instantiation, along with shared state tracking for known-down Redis instances. * Updated the Redis resilient command wrapper to proxy all commands through the appropriate policy (e.g. based on idempotency). * Implemented certain other Redis policy state detection.
91 lines
3.4 KiB
C#
91 lines
3.4 KiB
C#
/* Copyright (C) 2022-present Jube Holdings Limited.
|
|
*
|
|
* This file is part of Jube™ software.
|
|
*
|
|
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
|
|
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
|
|
* see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Jube.Cache.Redis
|
|
{
|
|
using Interfaces;
|
|
using log4net;
|
|
using MessagePack;
|
|
using Models;
|
|
using ResilientRedisConnection;
|
|
using Serialization;
|
|
using StackExchange.Redis;
|
|
|
|
public class CacheSanctionRepository(
|
|
ResilientRedisDatabase redisDatabase,
|
|
ILog log,
|
|
CommandFlags commandFlag = CommandFlags.FireAndForget) : ICacheSanctionRepository
|
|
{
|
|
public async Task<CacheSanction> GetByMultiPartStringDistanceThresholdAsync(int tenantRegistryId,
|
|
Guid entityAnalysisModelGuid, string multiPartString,
|
|
int distanceThreshold)
|
|
{
|
|
try
|
|
{
|
|
var redisKey = $"Sanction:{tenantRegistryId}:{entityAnalysisModelGuid:N}";
|
|
var redisHSetKey = $"{multiPartString}:{distanceThreshold}";
|
|
|
|
var hashValue = await redisDatabase.HashGetAsync(redisKey, redisHSetKey).ConfigureAwait(false);
|
|
|
|
if (!hashValue.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var sanction = MessagePackSerializer
|
|
.Deserialize<Sanction>(hashValue,
|
|
MessagePackSerializerOptionsHelper.StandardMessagePackSerializerWithCompressionOptions(false));
|
|
|
|
return new CacheSanction
|
|
{
|
|
CreatedDate = sanction.CreatedDate,
|
|
Value = sanction.Value
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error($"Cache Redis: Has created an exception as {ex}.");
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task InsertAsync(int tenantRegistryId, Guid entityAnalysisModelGuid, string multiPartString,
|
|
int distanceThreshold,
|
|
double? value)
|
|
{
|
|
try
|
|
{
|
|
var redisKey = $"Sanction:{tenantRegistryId}:{entityAnalysisModelGuid:N}";
|
|
var redisHSetKey = $"{multiPartString}:{distanceThreshold}";
|
|
|
|
var sanction = new Sanction
|
|
{
|
|
Value = value,
|
|
CreatedDate = DateTime.Now
|
|
};
|
|
|
|
var ms = new MemoryStream();
|
|
await MessagePackSerializer.SerializeAsync(ms, sanction,
|
|
MessagePackSerializerOptionsHelper.StandardMessagePackSerializerWithCompressionOptions(false)).ConfigureAwait(false);
|
|
await redisDatabase.HashSetAsync(redisKey, redisHSetKey, ms.ToArray(),
|
|
When.Always, commandFlag).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error($"Cache Redis: Has created an exception as {ex}.");
|
|
}
|
|
}
|
|
}
|
|
}
|