frostfs-sdk-csharp/src/FrostFS.SDK.Client/Mappers/OwnerId.cs
Pavel Gross 766f61a5f7
All checks were successful
DCO / DCO (pull_request) Successful in 47s
lint-build / dotnet8.0 (pull_request) Successful in 47s
lint-build / dotnet8.0 (push) Successful in 1m0s
[#26] All: Remove V2 from naming
Rename project, namespaces and class names

Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-11-18 11:33:50 +03:00

54 lines
No EOL
1.3 KiB
C#

using System;
using FrostFS.Refs;
using FrostFS.SDK.Cryptography;
using Google.Protobuf;
using Microsoft.Extensions.Caching.Memory;
namespace FrostFS.SDK.Client.Mappers.GRPC;
public static class OwnerIdMapper
{
private static readonly MemoryCacheEntryOptions _oneHourExpiration = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1))
.SetSize(1);
public static OwnerID ToMessage(this FrostFsOwner model)
{
if (model is null)
{
throw new ArgumentNullException(nameof(model));
}
if (!Caches.Owners.TryGetValue(model, out OwnerID? message))
{
message = new OwnerID
{
Value = ByteString.CopyFrom(model.ToHash())
};
Caches.Owners.Set(model, message, _oneHourExpiration);
}
return message!;
}
public static FrostFsOwner ToModel(this OwnerID message)
{
if (message is null)
{
throw new ArgumentNullException(nameof(message));
}
if (!Caches.Owners.TryGetValue(message, out FrostFsOwner? model))
{
model = new FrostFsOwner(Base58.Encode(message.Value.ToByteArray()));
Caches.Owners.Set(message, model, _oneHourExpiration);
}
return model!;
}
}