ServiceStack Redis如何实现分页

时间:2012-10-07 15:44:50

标签: c# redis servicestack

我试图找出如何在SS.Redis中进行分页,我使用:

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

它返回0,但我确定数据库不为空,因为r.GetAll()返回一个事物列表。这样做的正确方法是什么?


编辑:这是代码:

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());
    }
}

1 个答案:

答案 0 :(得分:2)

由于我没有看到任何代码,我假设您在添加entites时没有维护最近的列表。以下是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是另一个使用“最近”列表功能显示添加的最新问题的示例。每当创建新问题时,它都会通过调用AddToRecentsList来维护最近的问题列表。