zdrsh
zdrsh

Reputation: 1667

Stackexchange Redis - How to get large number of records paged

Is there a way to get the results paged. I'm using

 Database.HashGetAll(GetKey(key));

from the Stackexchange.Redis .Net Client but it throws OutOfMemory exception.

Upvotes: 2

Views: 3574

Answers (1)

Sam
Sam

Reputation: 381

You can use the HashScan method to iterate over a redis hash.

As you iterate through the result set, StackExchange.Redis will automatically call HSCAN to return more pages of results as required. You can optionally use the pageSize parameter to influence the number of records returned by each call to HSCAN.

var hashEntries = database.HashScan(key);

foreach (var entry in hashEntries)
{
    // ...
}

At any point during the iteration you can get the current value of the cursor, which would allow you to start a new iteration from the same page of results.

((IScanningCursor) hashEntries).Cursor

Have a look at http://redis.io/commands/scan for more details about how it works under the hood.

Number of elements returned at every SCAN call

SCAN family functions do not guarantee that the number of elements returned per call are in a given range. The commands are also allowed to return zero elements, and the client should not consider the iteration complete as long as the returned cursor is not zero.

The COUNT option

While SCAN does not provide guarantees about the number of elements returned at every iteration, it is possible to empirically adjust the behavior of SCAN using the COUNT option. Basically with COUNT the user specified the amount of work that should be done at every call in order to retrieve elements from the collection. This is just an hint for the implementation, however generally speaking this is what you could expect most of the times from the implementation.

  • The default COUNT value is 10.
  • When iterating the key space, or a Set, Hash or Sorted Set that is big enough to be represented by a hash table, assuming no MATCH option is used, the server will usually return count or a bit more than count elements per call.

Upvotes: 2

Related Questions