StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.87k stars 1.51k forks source link

Change value type of KeyRestore from byte[] to ReadOnlyMemory<byte> #2523

Open pairbit opened 1 year ago

pairbit commented 1 year ago

Change please type byte[] value

from

void KeyRestore(RedisKey key, byte[] value, TimeSpan? expiry = null, CommandFlags flags = CommandFlags.None) Task KeyRestoreAsync(RedisKey key, byte[] value, TimeSpan? expiry = null, CommandFlags flags = CommandFlags.None)

to

void KeyRestore(RedisKey key, ReadOnlyMemory value, TimeSpan? expiry = null, CommandFlags flags = CommandFlags.None) Task KeyRestoreAsync(RedisKey key, ReadOnlyMemory value, TimeSpan? expiry = null, CommandFlags flags = CommandFlags.None)

mgravell commented 1 year ago

Usually, this command is used in tandem with KeyDump, so you'd also want parity there. What's the scenario where this would be useful? For example, for StringGet there is a lease-based overload. One option here might be to add a lease-based KeyDump, which could then meaningfully be used with KeyRestore. However, I'm curious as to what your scenario is where you want a non-vector payload.

pairbit commented 1 year ago

I have developed program to restore a backup from file. I need to recover many keys. I would like to avoid multiple memory allocation for the byte array of the value. I want to replace "new byte[]" with using buffer or ArrayPool.

Code example as of now:

var value = new byte[valueSize];
await stream.ReadAsync(value, 0, valueSize);
await db.KeyRestoreAsync(key, value);

How I would like:

var rented = ArrayPool<byte>.Shared.Rent(valueSize);
var rentedMemory = rented.AsMemory(0, valueSize);
await stream.ReadAsync(rentedMemory);
await db.KeyRestoreAsync(key, rentedMemory);
ArrayPool<byte>.Shared.Return(rented);