将标记值存储到 Plone 内容中的最佳方法是什么:zope.annotation 或 setattr
对于开发人员来说,这是一个常见的情况。您希望为网站上需要存储数据的每种内容类型添加一项功能。它是一种元数据,而不是配置数据。
我看到两个解决方案:
- zope.annotation
- setattr: add attribute to the permanent object
我真的不知道为什么,但从 Plone2.5 开始,使用 zope.annotation 很好,现在它似乎不是存储附加数据的首选方式。例如 plone.uuid 使用 setattr 来存储唯一 id。
This is a common case for a developer. You want to add a feature for every content types of your website that need to store data. It is a kind of metadata, not configuration data.
I see two solutions:
- zope.annotation
- setattr: add attribute to the persistent object
I don't really know why but from Plone2.5 it was nice to use zope.annotation and now it seems to not be the prefered way to store additionnal data. For example plone.uuid use setattr to store the unique id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论是哪一种,都取决于数据更改的频率以及数据的大小。这完全取决于 ZODB 如何存储这些信息。
使用 setattr 将数据直接存储在对象上意味着数据存储在该对象的持久记录中。如果这是一个大对象,则意味着该写入将发生一个大事务。
将数据存储在 zope.annotations 注释中意味着您将为每个注释条目获得单独的持久记录,因此对数据的任何更改都将导致较小的事务。但是,如果您想经常访问这些数据,则需要在所有其他持久记录之上加载额外的持久记录。它将在您的 ZODB 缓存中占据一个位置,您的 ZEO 服务器或 RelStorage 服务器将需要为其提供服务,等等。
plone.uuid
使用 setattr 因为它通常只为给定对象生成一次,通常在某个时候它已经被创建了。它也是一个经常访问且非常小的数据。因此,通过将其直接放在对象本身上,一旦您加载该对象,它就会被加载,无需额外访问 ZODB,并且它在其生命周期中只会更改一次。注意:上面假设注释是使用 AttributeAnnotations 适配器存储的,这是最常见的方法,也是 Plone 内容的默认方法。
Either one, depending on how often your data changes and how big it is. It's all about how the ZODB stores this information.
Storing data directly on the object using setattr means that data is stored in the persistent record for that object. If that's a big object that means there is a big transaction going to take place for that write.
Storing data in a zope.annotations annotation means you get a separate persistent record for each annotation entry, so any changes to your data will result in a smaller transaction. But if you want to access this data often, that extra persistent record will need to be loaded, on top of all the other persistent records. It'll take a slot in your ZODB cache, your ZEO server or RelStorage server will need to serve it, etc.
plone.uuid
uses setattr because it is generally generated only once for a given object, usually at a time it is being created already. It is also a piece of data that is accessed often and quite small. So, by putting it directly on the object itself it'll be loaded as soon as you load that object, no extra trips to the ZODB required, and it'll only be changed once in it's lifetime.Note: the above presumes that the annotations are stored with the AttributeAnnotations adapter, which is the most common method and the default for Plone content.