In a dedicated cache, I can do a WeakHashMap to allow the JVM to release cached stuff if it isn't in use currently. In the (rather specific) case that you need that and the ES doesn't have a way to use a WeakHashMap instead of a HashMap, the ES will actually be more complicated because the component will have to store the key instead of the cached object directly.
because the component will have to store the key instead of the cached object directly.
… look like you do a guess here, “component will have to store the key”, is implementation depend.
So “Cache” can be considered as “how to retrieve an old created object efficiently” or more realistic and helpful “how to query- ask for a computed result again efficiently”.
Provide Cache method as a ChangeSet also an implementation among of others. but it’s obvious and efficient. Some ES, ORM did implemented ChangeSet, and have some special data structure, mostly in form of “key-value” collection to help better query, better lookup. Serialization come last, because I don’t want to go to deep into detail.
As my experience, some entity system implementations for game, especially mobile game (private used) is not very “generic” and “pure”. They use a lot of hard-linked group of Components, Aspects to have better query and better cache result. For things that should be retrieve in say 100 frames, they cache them like they should [Note: it’s very practical and useful to profile and tuneup upon run-time data and performance to see how much CPU, GPU through put and delay duration is appropriate, 100 is an bad example].
For example: We have thousands of Soldier NPC, each have 3 State(s) in a FSM AI, Stand, Run, Die. Stand and Run is used a lot from time to time, to create them from a “Template” or “Prefab” in real-time is not suitable because of number of creation for thousands entities is affordable. Therefore, using a cached component seem to be suitable in this scenerio. And also because “Component” in ES (if doing right) will almost have a “stateless” “memoryless” “timeless” attribute, they are very suite for this kind of job- so call cache efficient. And because of “stateless” is a little bit better than ORM’s Entity, whose component have to be stateful to support transaction and object-database synchronization (I’ve never tried db4o).
In my POV, talking about cache in ES, we’ve moved from concept to practice. A lot of “real” usecases refuse that “generic solution” is helpful but the mixing solution such as bringing cache and problematic hard-link, ad-hoc scripted link components and stuffs (dangerously break ES contract for concurency), Unity is one obvious example… but hey, we are making game anyway, aren’t we?