Tom
Tom

Reputation: 16246

ServiceStack Redis how to implement paging

I am trying to find out how to do paging in SS.Redis, I use:

var todos = RedisManager.ExecAs<Todo>(r => r.GetLatestFromRecentsList(skip,take));

it returns 0, but i am sure the database is not empty, because r.GetAll() returns a list of things. What is the correct way to do this?


EDIT: Here is the code:

public class ToDoRepository : IToDoRepository
{

    public IRedisClientsManager RedisManager { get; set; }  //Injected by IOC

    public Todo GetById(long id) {
        return RedisManager.ExecAs<Todo>(r => r.GetById(id));
    }
    public IList<Todo> GetAll() {
        return RedisManager.ExecAs<Todo>(r => r.GetAll());
    }
    public IList<Todo> GetAll(int from, int to) {
        var todos = RedisManager.ExecAs<Todo>(r => r.GetLatestFromRecentsList(from,to));
        return todos;
    }
    public Todo NewOrUpdate(Todo todo) {
        RedisManager.ExecAs<Todo>(r =>
        {
            if (todo.Id == default(long)) todo.Id = r.GetNextSequence(); //Get next id for new todos 
            r.Store(todo); //save new or update
        });
        return todo;
    }
    public void DeleteById(long id) {
        RedisManager.ExecAs<Todo>(r => r.DeleteById(id));
    }
    public void DeleteAll() {
        RedisManager.ExecAs<Todo>(r => r.DeleteAll());
    }
}

Upvotes: 1

Views: 1735

Answers (1)

mythz
mythz

Reputation: 143339

As I don't see any code, I'm assuming you're not maintaining the Recents list when you're adding the entites. Here's the test case for GetLatestFromRecentsList:

var redisAnswers = Redis.As<Answer>();

redisAnswers.StoreAll(q1Answers);
q1Answers.ForEach(redisAnswers.AddToRecentsList); //Adds to the Recents List

var latest3Answers = redisAnswers.GetLatestFromRecentsList(0, 3);

var i = q1Answers.Count;
var expectedAnswers = new List<Answer>
{
    q1Answers[--i], q1Answers[--i], q1Answers[--i],
};

Assert.That(expectedAnswers.EquivalentTo(latest3Answers));

Redis StackOverflow is another example that uses the Recents list feature to show the latest Questions added. It maintains the recent list of questions by calling AddToRecentsList whenever a new Question is created.

Upvotes: 2

Related Questions