将长时间运行的任务的结果与之前的结果进行比较
我有一个 Web 服务,它返回一个复杂的数据结构,表示密集过程的结果。为了使服务能够响应,该进程在异步任务中运行。
第一次调用服务时,将返回一个空数据结构并启动任务。后续服务调用将返回缓存的数据,直到任务完成。完成后,我需要确定结果是否确实发生了变化。如果是这样,我会更新一个属性,该属性指示结果更新的日期/时间,并重置缓存的数据,以便服务返回新的结构。在未来的某个时刻,基于各种原因,Task会重新启动,逻辑会重复。
我正在寻找最有效的方法来确定结果是否已从“缓存”数据中更改。最简单的方法是在结构中的每个对象上实现 Equals,其中父对象的相等性基于其子对象的相等性等。因此,在根对象上调用 Equals 将产生所需的结果,但这需要爬行整个结构,我不确定这是最好的方法。
我还考虑过从原始副本开始,并使用“IsDirty”标志来指示结构中的对象已更改,但担心复制操作可能会抵消任何性能优势。
您会使用什么方法(以及为什么)?
I have a web service that returns a complex data structure representing the results of an intensive process. To make the service responsive, the process is run in an asynchronous Task.
The first time the service is called, an empty data structure is returned and the Task started. Subsequent service calls return the cached data until the Task completes. When finished, I need to determine if the results actually changed. If so, I update a property that indicates the date/time the results were updated and reset the cached data so the new structure is returned by the service. At some point in the future, based on a variety of reasons, the Task is restarted and the logic repeats.
I am looking for the most efficient way of determining whether the results have changed from the 'cached' data. The simplest would be to implement Equals on every object in the structure with parent's equality based on their children's equality, etc. so calling Equals on the root objects would yield the desired result, but this requires crawling the entire structure and I'm not sure this is the best approach.
I also thought about starting with a copy of the original and using 'IsDirty' flags to indicate that a object in the structure has changed but worry that the copy operation could negate any performce benefits.
What approach would you use (and why)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个静态方法,递归地遍历 2 个实例( left 和 right )的所有属性,可以选择使用属性来控制这些属性,并调用 left over right 的 equals 方法。当其中一个属性表明不相等时,我们就知道这两个实例是不同的。这使我能够实现最少量的比较代码,同时完全控制比较内容和比较方式(通过使用 [NoCompare] 等新属性标记某些属性并实现自定义相等逻辑)。
I would create a static method that recursively walks over all properties of 2 instances ( left and right ), which optionally can be controlled by using attributes, and calls the equals method of left over right. When one of the properties indicate inequality, we know that the 2 instances are different. This allows me to implement a minimum amount of code for comparison yet be fully in control in what gets compared and how it gets compared (by marking certain properties with a new attribute like [NoCompare] and by implementing custom equality logic).
假设您已经为数据对象提供了 equals 运算符,您应该能够检查它们是否相等。
例如:
然后当你得到结果时:
注意:线程安全检查被遗漏了。
Assuming you've provided the equals operator for your data object, you should be able to just check check that they're equal.
eg:
Then when you've got your result back:
nb: Thread-safety checking left out.