我正在尝试通过Servicestack.Redis将Redis Cache用于MVC应用程序。
Redis仅适用于Key字段(默认Id)。我有以下课程
[DataContract]
public partial class Author_Book
{
public Author_Book() { }
public Author_Book(int id, string Title, int AuthorID)
{
this.Id = id;
this.Title = Title;
this.AuthorId = AuthorID;
}
[DataMember]
public int Id { get; set; }
[DataMember]
public int AuthorId { get; set; }
[DataMember]
public string Title { get; set; }
public virtual Author Author { get; set; }
}
[DataContract]
public partial class Author
{
public Author()
{
this.Author_Book = new HashSet<Author_Book>();
}
public Author(int id, string name)
{
this.Id = id;
this.Name = name;
this.Author_Book = new HashSet<Author_Book>();
}
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public virtual ICollection<Author_Book> Author_Book { get; set; }
}
以下功能进行测试
public static void ListTest()
{
// configure Redis to use AuthorID as ID field for Author_Book entities
ServiceStack.ModelConfig<Author_Book>.Id(x => x.AuthorId);
PooledRedisClientManager redisManager = new PooledRedisClientManager("localhost:6379");
Author auth = new Author(1, "Author-1");
Author auth2 = new Author(2, "Author-2");
Author_Book ab1 = new Author_Book(1, "Book-1", 1);
Author_Book ab2 = new Author_Book(2, "Book-2", 2);
Author_Book ab3 = new Author_Book(3, "Book-3", 2);
IList<Author_Book> retList;
using(IRedisClient rc = redisManager.GetClient())
{
// store Authors
rc.Store<Author>(auth);
rc.Store<Author>(auth2);
// store Author Books
rc.Store<Author_Book>(ab1);
rc.Store<Author_Book>(ab2);
rc.Store<Author_Book>(ab3);
// Get data back from redis
List<int> ids = new List<int>();
ids.Add(2);
retList = rc.GetByIds<Author_Book>(ids);
}
foreach(Author_Book ab in retList)
{
Console.WriteLine(ab.Title);
}
Console.Read();
}
我在这里要做的是,
Redis
配置为使用AuthorID
作为Author_Book实体的Key字段。问题是,它只给我Book-3
,预期结果为Book-2
和Book-3
。
我猜rc.Store<Author_Book>(ab3);
正在覆盖之前的记录,因为ab2
和ab3
都有相同的作者ID。
如何实现它?
我想存储单独的实体列表而不是Graph,以便稍后可以单独更新/删除它们。