# Make sure to always return a clean document from the cache

**URL:** <https://forum.xwiki.org/t/make-sure-to-always-return-a-clean-document-from-the-cache/15562>\
**Category:** Development\
**Tags:** proposal\
**Created:** [September 17, 2024, 2:24pm UTC](https://forum.xwiki.org/t/make-sure-to-always-return-a-clean-document-from-the-cache/15562 "2024-09-17T14:24:03Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [September 17, 2024, 2:24pm UTC](https://forum.xwiki.org/t/make-sure-to-always-return-a-clean-document-from-the-cache/15562/1 "2024-09-17T14:24:03Z")

</div>

Hi devs,

Directly modifying the XWikiDocument instance coming from the cache always end up causing some problems, we know that and that’s why we generally try to clone it before modifying it.

The problem is that it’s just `generally`, and we keep finding this mistake, in extensions or even in XWiki Standard (just identified and fixed [a very old one](https://jira.xwiki.org/browse/XWIKI-22511)) so I’m trying to find of a good way to make sure this does not happen again.

I can think of two possibilities, in XWikiCacheStore#loadXWikiDoc:

1. always return a cloned version of the XWikiDocument
2. before returning the XWikiDocument, check if it was modified (XWikiDocument#isMetadataDirty) and reload it from the database if that’s the case

WDYT ?

While `1.` is obviously much simpler and safer, I’m afraid it might be too expensive in terms of performances, and it also might cause more retro compatibility problems (all the reasons why we never did it so far), so right now I would be pushing for `2.` (but it’s not a -1 for `1.` if the majority think it’s better).

Of course, it feels way too dangerous to apply that to any other branch than master.

---

_[View the full topic](https://forum.xwiki.org/t/make-sure-to-always-return-a-clean-document-from-the-cache/15562)._
