gpt4 book ai didi

c# - ServiceStack Redis如何实现分页

转载 作者:IT王子 更新时间:2023-10-29 06:12:11 26 4
gpt4 key购买 nike

我正在尝试找出如何在 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());
}
}

最佳答案

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

关于c# - ServiceStack Redis如何实现分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12770251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com