问题
The javadocs for CacheStoreMode differentiate in a point I cannot really grasp:
The javadocs for the USE
mode:
Insert/update entity data into cache when read from database and when committed into database: this is the default behavior. Does not force refresh of already cached items when reading from database.
The javadocs for the REFRESH
mode differ in the last sentence:
Forces refresh of cache for items read from database.
When an existing cached entity instance is updated when reading from database, this would typically involve overwriting the existing data. So what is the difference between forcing and not forcing a refresh in this context?
Thank you.
回答1:
As far as I know:
CacheStoreMode.USE
should be used if a given EntityManagerFactory has an exclusive write-access to the underlying database thus it implies that there is no chance for an entity instance stored in the shared cache to be stale.CacheStoreMode.REFRESH
should be enabled if the underlying database might be accessed by multiple commiters (i.e. EntityManagerFactory instances, applications in different JVMs, external JDBC sources) thus an entity instance stored in the shared cache may become stale.
Since CacheStoreMode.USE does not force refresh of already cached entities when reading from the database, CacheStoreMode.REFRESH does.
回答2:
I think it will make difference where the most recent updated data from the database is needed, where it gets updated from the back-end, not through the application.
In my application, its the same case (but not using any cache strategy), where we have to load all data each time; as it gets modified implicitly through messaging from the external system, else we will be dealing with the stale data.
There might be few cases where scheduled jobs, external systems etc. update database directly there CacheStoreMode.REFRESH
is appropriate; while for normal case CacheStoreMode.USE
.
[Other than this I can't recollect any other cases, where it might make difference between these two modes]
Edit: The documentation seems confusing & too short to explain properly. Also, in case of native queries, bulk updates etc. items are skipped & aren't cached.
CacheStoreMode.USE
: Only new items are put into the cache, not for already cached ones.
CacheStoreMode.REFRESH
: New items are put into the cache & already existing cached items are refreshed.
来源:https://stackoverflow.com/questions/23422959/what-is-the-difference-between-cachestoremode-use-and-refresh