gpt4 book ai didi

ruby-on-rails - Ohm & Redis : when to use set, 列表或集合?

转载 作者:IT王子 更新时间:2023-10-29 05:57:37 25 4
gpt4 key购买 nike

使用 Ohm 和 Redis 时,集合与集合或列表有什么区别?

几个 Ohm 示例使用列表而不是集合(参见 list doc itself):

class Post < Ohm::Model
list :comments, Comment
end

class Comment < Ohm::Model
end

这种设计选择的基本原理是什么?

最佳答案

只是为了扩展 Ariejan 的回答。

  • 列表 - 有序。类似于 Ruby 中的数组。用于排队和保持元素有序。

  • 集合 - 一个无序列表。它的行为类似于 Ruby 中的数组,但针对更快的查找进行了优化。

  • 集合 - 与引用结合使用,它提供了一种表示关联的简单方法。

本质上,集合和引用是处理关联的便捷方法。所以这个:

class Post < Ohm::Model
attribute :title
attribute :body
collection :comments, Comment
end

class Comment < Ohm::Model
attribute :body
reference :post, Post
end

是以下内容的快捷方式:

class Post < Ohm::Model
attribute :title
attribute :body

def comments
Comment.find(:post_id => self.id)
end
end

class Comment < Ohm::Model
attribute :body
attribute :post_id
index :post_id

def post=(post)
self.post_id = post.id
end

def post
Post[post_id]
end
end

为了回答您关于设计选择的基本原理的原始问题 - 引入了集合和引用以提供用于表示关联的简单 API。

关于ruby-on-rails - Ohm & Redis : when to use set, 列表或集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781605/

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