Example #1
0
    public static async ValueTask <TValue?> TryGetValueAsync <TValue>(this IKeyValueStorage <string> storage, string key, CancellationToken cancellationToken = default)
        where TValue : IRocketMessage <TValue>
    {
        var bytesPool = BytesPool.Shared;

        using var bytesPipe = new BytesPipe(bytesPool);

        if (!await storage.TryReadAsync(key, bytesPipe.Writer, cancellationToken))
        {
            return(default);
Example #2
0
    public static async IAsyncEnumerable <TValue> GetValuesAsync <TValue>(this IKeyValueStorage <string> storage, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        where TValue : IRocketMessage <TValue>
    {
        var bytesPool = BytesPool.Shared;

        using var bytesPipe = new BytesPipe(bytesPool);

        await foreach (var key in storage.GetKeysAsync(cancellationToken))
        {
            if (!await storage.TryReadAsync(key, bytesPipe.Writer, cancellationToken))
            {
                continue;
            }

            var value = IRocketMessage <TValue> .Import(bytesPipe.Reader.GetSequence(), bytesPool);

            yield return(value);

            bytesPipe.Reset();
        }
    }