1

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());
    }
}
Tom
  • 15,781
  • 14
  • 69
  • 111

1 Answers1

2

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Looks like I don't understand the meaning of the functions I am using. Where can we get the SS.Redis documentation to explain how SS Redis functions and data are sctructured? – Tom Oct 08 '12 at 01:55
  • There's plenty of docs around, start with: http://www.servicestack.net/docs/category/Redis%20Client then the links contained in http://stackoverflow.com/a/8919931/85785 - then look at the unit tests if still unsure. – mythz Oct 08 '12 at 02:09
  • May I rephrase my q again... When I use the r.Store(todo); how do I get paged items only, but not to call the r.GetAll(); ... Secondly, I am not sure what is the usage of RecentsList feature. I can only guess it can access the list you have just recently used. If I turn off redis-server, I can no longer get my todo list that way. So if I want a paged todo list, calling RecentsList may not guarantee outcome, I may need to call another function to do that, Right? – Tom Oct 08 '12 at 04:39
  • Read through this answer carefully to see how items are stored in Redis: http://stackoverflow.com/a/8919931/85785 Basically it's stored in a Key/Value dictionary that doesn't contain order. You need to maintain something like the in-built RecentsList feature (described above) to maintain an ordered list of most recent ids added. The paging shown above just grabs the most recent 3 ids and fetches the entities for them. In short, if you need paging use the Recents feature or implement your own using Redis lists or sorted set. – mythz Oct 08 '12 at 04:41