Hi devs,
Currently the getDocument method always goes to the storage to retrieve
the document, even if the same document has just been retrieved. This
means that the following code will not work:
#set($d = $xwiki.getDocument('X'))
$d.setTitle('the title')
$xwiki.getDocument('X').getTitle() # will not print 'the title'
I'd like to change getDocument so that it first searches in a map of
used documents in the current context. This means the following:
- getDocument searches in XWikiContext.usedDocuments (or better, in the
ExecutionContext)
- if found, return the value from there
- if not, go to the storage, return it to the caller
- when the document is changed for the first time, i.e. when
api.Document.getDoc() is called, clone the original document and put it
in usedDocuments
- as a special case, PreviewAction also puts the updated context
document in usedDocuments
This means that consecutive calls for retrieving a (changed) document
will always return the same object. This prevents possible preview bugs,
like
http://jira.xwiki.org/jira/browse/XABLOG-14 or
http://jira.xwiki.org/jira/browse/XWIKI-4689
Yet this is an important behavior change. Do you think anybody is using
this "feature", and actually expects the above code example to work as
it does now?
Also, we must be careful with the performance, since this new map could
get big, holding all the documents in the database. Perhaps a LRU
fixed-size map would be better, although this breaks the uniqueness
guarantee.
So, WDYT?
1. Should we introduce this cache?
2. Should it be limited in size?
--
Sergiu Dumitriu
http://purl.org/net/sergiu/