Caching - gkresic/commons-fields GitHub Wiki

Caching is hard. Not as hard as naming things, but still... hard.

However, having the ability to cache only partial objects, cache invalidation can be greatly simplified, since we can decide to cache only attributes we can easily invalidate. In such scenario, when caller requests object with attributes which are subset of attributes cache supports, whole object could be resolved from cache, without any roundtrip to the backing store. In case caller also requested some attributes not supported by cache, only those non-cached attributes will be fetched on demand.

Meet FieldsEntityCache and related CachingCRUDFieldsService on how to use such cache.

FieldsEntityCache requires you to specify which fields to cache and, optionally, which fields to precache. Use precaching in cases when backing service have roughly the same performance when fetching one vs fetching many attributes, such is the case when reading row from relational database where major cost is locating row on disk, after which reading actual columns from that row is rather fast.

Note that FieldsEntityCache in its current form caches objects using simple Map. While this is good enough for short-lived caches (like inside one HTTP request or one database transaction), long-lived caches should have more advanced approach (like, for example, using Google Guava's cache implementation). Pull requests are welcome :)