[#20] Client: Optimize memory usage

Avoid memory allocation, use cache and static

Signed-off-by: Pavel Gross <p.gross@yando.com>
This commit is contained in:
Pavel Gross 2024-08-01 16:17:36 +03:00
parent 35fe791406
commit 0ddde467cd
46 changed files with 596 additions and 372 deletions

View file

@ -1,21 +1,42 @@
using FrostFS.Refs;
using FrostFS.SDK.Cryptography;
using FrostFS.SDK.ModelsV2;
using Google.Protobuf;
using Microsoft.Extensions.Caching.Memory;
using System;
namespace FrostFS.SDK.ClientV2.Mappers.GRPC;
public static class OwnerIdMapper
{
public static OwnerID ToGrpcMessage(this OwnerId ownerId)
private static readonly MemoryCacheEntryOptions _oneHourExpiration = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromHours(1))
.SetSize(1);
public static OwnerID ToMessage(this OwnerId model)
{
return new OwnerID
if (!Cache.Owners.TryGetValue(model, out OwnerID? message))
{
Value = ByteString.CopyFrom(ownerId.ToHash())
};
message = new OwnerID
{
Value = ByteString.CopyFrom(model.ToHash())
};
Cache.Owners.Set(model, message, _oneHourExpiration);
}
return message!;
}
public static OwnerId ToModel(this OwnerID ownerId)
public static OwnerId ToModel(this OwnerID message)
{
return new OwnerId(ownerId.ToString());
if (!Cache.Owners.TryGetValue(message, out OwnerId? model))
{
model = new OwnerId(Base58.Encode(message.Value.ToByteArray()));
Cache.Owners.Set(message, model, _oneHourExpiration);
}
return model!;
}
}