ServiceStack.Net Redis的:存储相关对象与相关对象ID对象、Net、ServiceStack、ID

2023-09-02 01:24:33 作者:[VIP]的冷冻期

我的团队已经决定与Redis的通过ServiceStack.net Redis的客户作为我们工作的一个新的大容量网站的底层存储库工作。我真的不知道到哪里寻找文档这个问题(无论是普通Redis的文档或特定ServiceStack.Net文档或两者) - 是实际存在一个明确的来源为如何实现Redis的文件通过ServiceStack.Net,其中包括所有你需要知道的关于这两个Redis的概念和ServiceStack.Net概念,还是我们需要从两个方面分别集成文档,以获取完整的画面?

My team has decided to work with Redis via the ServiceStack.net Redis Client as an underlying repository for a new high-volume website we're working on. I'm not really sure where to look for documentation for this question (either for general Redis docs or specific ServiceStack.Net docs or both) - is there actually a definitive source for documentation on how to implement a Redis via ServiceStack.Net that includes all you need to know about both Redis concepts and ServiceStack.Net concepts, or do we need to integrate documentation from both aspects separately to get the full picture?.

我只是在努力解决如何储存相关对象在我们的模型的对象图。下面是我要使用一个简单的场景:

I'm just grappling with how exactly to store related objects in our model's object graph. Here's a simple scenario that I want to work with:

有系统的两个对象:用户订阅。在RDBMS而言这两个对象有一个一对多的关系,即,一个用户订阅对象和饲料只能属于一个用户。饲料将始终从Redis的通过他们的用户进行访问,但偶尔我们会希望通过饲料的实例来获得访问用户。

There are two objects in the system: User and Feed. In RDBMS terms these two objects have a one-to-many relationship, that is, a User has a collection of Feed objects and a feed can only belong to one User. Feeds will always be accessed from Redis via their user but occasionally we'll want to get access to the user via a feed instance.

所以这个问题我已经是我们是否应该保存相关的对象作为属性还是应该保存编号相关对象的价值?为了说明:

So the question I have is whether we should be storing the related objects as properties or should we store the Id values of the related objects? To illustrate:

办法A

public class User
{
    public User()
    {
        Feeds = new List<Feed>();
    }

    public int Id { get; set; }

    public List<Feed> Feeds { get; set; }

    // Other properties
}

public class Feed
{
    public long Id { get; set; }

    public User User { get; set; }
}

办法乙

public class User
{
    public User()
    {
        FeedIds = new List<long>();
    }

    public long Id { get; set; }

    public List<long> FeedIds { get; set; } 

    public List<Feed> GetFeeds()
    {
        return repository.GetFeeds( FeedIds );
    }
}

public class Feed
{
    public long Id { get; set; }

    public long UserId { get; set; }

    public User GetUser()
    {
        return repository.GetUser( UserId );
    }
}

这上面的方法将工作最好?我见过各种示例中使用了这两种方法,但我得到的IM pression,一些我见过的例子可能不是最佳做法。

Which of the above approaches will work best? I've seen both approaches used in various examples but I get the impression that some of the examples I've seen may not be best-practice.

几个简单的相关的问题:

A few simple related questions:

如果我更改了一个对象会自动将其反映在Redis的还是会需要保存?我假设后者,但需要绝对清晰。 如果我(可以)使用方法A,将更新到用户对象X在整个对象图反映无论它是引用还是会需要保存整个图形的变化? 有没有通过它存储的对象的问题界面(即使用的IList&LT;饲料&GT; ,而不是名单,其中,进料&GT; ? If I make a change to an object will it automatically be reflected in Redis or will it require a save? I'm assuming the latter, but need to be absolutely clear. If I (can) use Approach A, will an update to User object X be reflected throughout the entire object graph wherever it is referenced or will it be necessary to save changes across the graph? Is there a problem with storing an object via it's interface (i.e. use IList<Feed> as opposed to List<Feed>?

很抱歉,如果这些问题都有点基本的 - 直到2个星期前我从来没有听说过的Redis的 - 更不用说ServiceStack - (也没有任何人在我的球队),所以我们真的从零开始这里出发......

Sorry if these questions are a little basic - until 2 weeks ago I'd never even heard of Redis - let alone ServiceStack - (nor had anyone in my team) so we're really starting from scratch here...

推荐答案

而不是重新哈希很多其他的文档是在那里在野外,我将列出了几个围绕周边的Redis + ServiceStack的Redis的一些背景资料客户端:

Rather than re-hash a lot of other documentation that's out there in the wild, I'll list a couple around for some background info around Redis + ServiceStack's Redis Client:

如何看待设计的NoSQL Redis的应用程序时 设计使用Redis的 一个NoSQL数据库 Redis的总体概述和.NET 无模式版本,并使用C#Redis的客户 What to think about when designing a NoSQL Redis application Designing a NoSQL Database using Redis General Overview of Redis and .NET Schemaless versioning and Data Migrations with C# Redis Client

首先,我想指出的是,使用Redis的作为数据存储只是提供了一个空白的画布,本身不具有相关实体的任何概念。也就是说,它只是提供了访问分布式COMP-SCI的数据结构。如何关系获取存储最终是由客户端驱动程序(即ServiceStack C#Redis的客户端)或应用程序开发人员,使用Redis的的原始数据结构的操作。由于所有主要的数据结构在Redis的实现,你基本上你想如何构建和存储你的数据完全的自由。

First I want to point out that using Redis as a data store just provides a blank canvas and doesn't have any concept of related entities by itself. i.e. it just provides access to distributed comp-sci data structures. How relationships get stored is ultimately up to the client driver (i.e. ServiceStack C# Redis Client) or the app developer, by using Redis's primitive data structure operations. Since all the major data structures are implemented in Redis, you basically have complete freedom on how you want to structure and store your data.

所以最好的方法去思考如何存储的东西,在Redis的,是完全无视有关数据如何存储在一个关系型数据库表,并想想如何利用存储在code,即内置在内存中的C#集合类 - 这Redis的镜子在自己的服务器端数据结构的行为

So the best way to think about how to store stuff in Redis, is to completely disregard about how data is stored in an RDBMS table and think about how it is stored in your code, i.e. using the built-in C# collection classes in memory - which Redis mirrors in behavior with their server-side data-structures.

尽管没有相关实体的概念,Redis的内置的设置和的SortedSet 数据结构提供存储索引的理想方式。例如。 Redis的的设置集合只存储一个最大的1出现的元素。这意味着你可以安全地添加项目/按键/ IDS它并不在乎,如果该项目已经存在,因为最终的结果是一样的了,你把它叫做1倍或100倍 - 也就是说,它的幂等,最终只有1元仍存储在设置。因此,存储的对象图(聚合根)时,常见的用例是被存储在儿童实体ID(也称为外键)为一组,每次保存模型的时间。

Despite not having a concept of related entities, Redis's built-in Set and SortedSet data structures provide the ideal way to store indexes. E.g. Redis's Set collection only stores a max of 1 occurrence of an element. This means you can safely add items/keys/ids to it and not care if the item exists already as the end result will be the same had you called it 1 or 100 times - i.e. it's idempotent, and ultimately only 1 element remains stored in the Set. So a common use-case is when storing an object graph (aggregate root) is to store the Child Entity Ids (aka Foreign Keys) into a Set every time you save the model.

有关如何实体都存储在Redis的我建议安装 Redis的管理UI 效果很好,良好的可视性与ServiceStack的C#Redis的客户端,因为它使用下面的关键命名惯例提供一个很好的分层视图,分组的类型的实体一起(尽管存在于同一个全局密钥空间所有键)。

For a good visualization of how Entities are stored in Redis I recommend installing the Redis Admin UI which works well with ServiceStack's C# Redis Client as it uses the key naming convention below to provide a nice hierarchical view, grouping your typed entities together (despite all keys existing in the same global keyspace).

要查看和编辑一个实体,点击修改链接,查看和修改所选的实体内部JSON重新presentation。希望你将能够就如何设计你的模型更好的决策,一旦你可以看到它们是如何存储的。

To view and edit an Entity, click on the Edit link to see and modify the selected entity's internal JSON representation. Hopefully you'll be able to make better decisions about how to design your models once you can see how they're stored.

在C#Redis的客户端适用于有一个单一的主键的任何波苏斯 - 在缺省情况下,预计将编号(虽然这个约定的