-
Notifications
You must be signed in to change notification settings - Fork 8
Caching
Moo caches translations within a translation session, so it's worth understanding how that works. At the moment, there's no published way to reuse a translation session -- each invocation of Moo from either Moo
or Translate
an Update
entry points results in a new session.
Translators are cached within the instance of Moo, which can be re-used multiple times. This gives you the means of using a single instance of Moo without incurring all the setup cost to parse configuration and scan classes for properties with each invocation.
When Moo first considers an object for translation or to be updated, it uses reflection of the class and its ancestors to learn the properties of that class and parse any configuration you may have added to the class through annotations. That work of parsing is cached within the CachingTranslatorFactory
, which itself is stored within an instance of Moo.
If you make several calls to Translate
in sequence with overlapping object types, Moo will have to repeat itself, doing the reflection and annotation parsing once for each call. On the other hand, if you create an instance of Moo
and invoke its translate()
method repeatedly, this reflection and parsing will only take place the first time each translator is needed.
When Moo does a full translation of an object, it caches the result. If the same translation is requested using the same translation session, Moo will reuse the earlier translation.
This ensures that if Moo were to follow an object cycle where A refers to B and B refers to C and C refers back to A, it wouldn't enter an infinite loop where it translates the same objects over and over.
It also means that if two parts of the same source graph refer to the same object, those same two parts of the resulting graph after translation will also refer to the same destination object -- that the shape of the graph is preserved even though the source and destination are actually different objects.
If you were to re-use the same translation session over widely different timeframes, you would risk the potential that the cached translation will have been modified and won't be accurate. As a result, you should be careful not to re-use translation sessions when they're not part of the same transaction. Moo deliberately avoids exposing these to you to reduce that risk.
- Five Minute Introduction
- Getting Moo
- Translating Object Properties
- Translating Collection Properties
- Translating Map Properties
- Source Expressions
- Access Modes
- Translating to Value Types
- Extensions
- External Objects in Translation
- Ignoring Properties
- Updating Collections
- Translation Target Factories
- Nested Updates
- Optional and Required Properties
- Order of Property Translation
- Constructors
- Lookup on Update
- Caching
Releases
Background