Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Lease APIs to less allocate memory #25

Open
neuecc opened this issue Dec 18, 2019 · 0 comments
Open

use Lease APIs to less allocate memory #25

neuecc opened this issue Dec 18, 2019 · 0 comments

Comments

@neuecc
Copy link
Collaborator

neuecc commented Dec 18, 2019

StachExchange.Redis 2.x has new lease APIs.
It can avoid byte[] allocation in deserialize.

using (var lease = conn.GetDatabase().StringGetLease("key"))
{
    return MessagePackSerializer.Deserialize<Foo>(lease.Memory);
}

For serialize, RedisValue accepts ReadOnlyMemory so we can write the following.

public Task<bool> async SetAsync<T>(T value, TimeSpan? expiry = null, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
    // ArrayBufferWriter is only for .NET Core 3.0 and it creates new T[]
    // create similar code that uses ArrayPool<byte>.Shared.Rent
    using(var bufferWriter = new ArrayBufferWriter<byte>())
    {
        MessagePackSerializer.Serialize(bufferWriter, value);
        RedisValue serialized = bufferWriter.WrittenMemory;
        return await this.Connection.Database.StringSetAsync(this.Key, serialized, expiry, when, flags);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant