参考 AI 的回答,用下面的代码实现了
public async IAsyncEnumerable<T> ReadRedisListAsync<T>(string listKey, int pageSize = 20)
{
long start = 0;
while (true)
{
long stop = start + pageSize - 1;
RedisValue[] values = await redis.Database.ListRangeAsync(listKey, start, stop);
if (values == null || values.Length == 0)
break;
foreach (var value in values)
{
var item = redis.Serializer.Deserialize<T>(value!);
if (item == null) continue;
yield return item;
}
start += pageSize;
if (values.Length < pageSize)
break;
}
}