ORM - XPO缓存的工作方式. 如何刷新持久对象

How the XPO cache works. How to refresh persistent objects.
XPO缓存的工作方式. 如何刷新持久对象

ArticleID: 643; Product: eXpress Persistent Objects; Version(s): 1; Technology: .NET; Updated: 11/29/2004

Description
问题描述

It's difficult to get the latest information from the underlying database: XPCollection.Reload doesn't reload the objects' content (properties), the XPObject.Reload reloads only the object' properties but doesn't reload all the related objects because all them are get from the cache.
从数据库获取最新信息是很困难的: XPCollection.Reload 不会刷新持久对象的内容(属性), XPObject.Reload 仅仅刷新它自己的属性,不会同步刷新其关联对象,所有这些都是因为它们被存入了Cache。

Solution
解决方案

There are several things about which you should be aware.
关于这个问题,有几件事你需要明白。

XPO uses an internal object cache for each operation it performs, wherever possible. For instance, every time you want to retrieve an object collection XPO queries the database for all objects that match the specified collection's criteria. While processing the objects in the query's results, XPO checks whether a given object already exists in the object cache. If the object is found in the object cache XPO initializes its properties with the data retrieved from the query's execution; otherwise, XPO creates the object's instance, initializes it and adds it to the object cache. The object is then added to the collection. The objects are preserved in the object cache until the application makes use of them. Once the application disposes of the object, the garbage collector can take care of it as the object cache uses the WeakReference class to reference the objects. To refresh the object's state or discard changes made to the object, use its own Reload method, i.e. xpObject.Reload().
在任何情况下,XPO都使用了内置对象缓存器来缓存它的所有操作。举个例子,当你试图查询一个对象集合,XPO将从数据库获取所有匹配的对象。在处理查询结果时,XPO会检查对象是否已经加入缓存,如果已加入,XPO将使用查询获得的数据更新该缓存对象的属性。如果在缓存器中没有找到该对象,XPO将创建新的对象实例,初始化相关数据并加入到缓存器。这些缓存对象引用被加入到集合返回。这些对象将一直保存在缓存器中,供系统调用。一旦系统释放它们,垃圾回收器(GC)就可以处理这些被缓存器引用的对象。要刷新对象状态或者放弃当前的修改,请使用其自身的Reload方法,如:xpObject.Reload()。

The object cache technique is very useful and effective for single-user applications and even lets you outperform ADO.NET classes. Though this technique may have the reverse effect in a multi-user application and instead hinder data retrieval. For these applications we recommend you split your application into several logical blocks that retrieve data and use an independent XPO's Session object in each logical block. In order to get up to date information from storage or discard changes made to the objects in the session, all you have to do is to create a new session object and retrieve objects from storage using this session.
在单用户程序中,对象缓存技术是非常实用而有效的,它甚至超过了ADO.NET。当然这种技术在多用户程序中存在一些副作用,阻碍了数据更新。针对这样的程序,我们建议你将程序划分为多个逻辑块,在每个逻辑块中使用独立的XPO Session去获取和调用持久对象。为了从存储器获取更新信息,或者放弃修改,你必须创建一个新的Session对象来替代当前正在使用的Session,实现从存储器上获取数据。

All that mean two things:
所有这些表明两件事:

Firstly, the "XPBaseObject.Reload" method just reloads the object properties. It performs a new query to the database, fetches the values for the all object' properties and write these values to the appropriate properties. The associated objects will not be reloaded, so if you want to reload them you should call the "Reload" method again. Such an application design will impact performance: to reload values from the database will take a lot of time. Within our applications we use a new XPO Session: we know the identifier of the desired object and just get it from another session. It looks like a workaround and it is a workaround: we are planning to change the Session design to simplify the objects reloading and cancelling (it looks like the reloading).
首先,"XPBaseObject.Reload"方法仅仅更新其自身的属性。它向数据库执行一个新的查询,取回对象的所有属性数据,并写入这些属性。关联对象并不会被刷新,因此,如果你希望更新更新它们,你必须再次调用它们的"Reload"方法。这样的程序设计会影响性能,从数据库刷新数据会耗费大量的时间。在我们的程序内部我们使用一个新的Session来处理: 我们知道希望获取的对象标识,并使用另外一个Session去得到它。这看上去象一个工作计划,事实上它确实是一个计划:我们计划改变Session设计,使得对象能够简单地被刷新或取消(看上去象刷新)。

Secondly, the "XPCollection.Reload" method can be used to refresh only the scope of objects, and it is not applicable to reload the objects' content.
其次,"XPCollection.Reload" 方法仅刷新其搜索结果,而不是用来刷新对象内容。
posted @ 2009-09-23 00:40  咖︷喱  阅读(625)  评论(0)    收藏  举报