Reputation: 127
A couple of days ago i implemented Caching feature in Spring framework in a project and that was interesting but now this question has been brought to my mind that what is difference of using Spring Cache and fetching or initializing data on application startup and keeping that in a variable whitin a spring bean which can be accessed via a getter?
Upvotes: 1
Views: 1323
Reputation: 5492
If you have a single application instance, you don't care about any of more complex features (listed below) and you synchronize access to the variable correctly for multi-threaded use, there's no difference. Cache is cache - you have temporarily stored a value that is otherwise expensive to calculate or fetch.
However, Spring Cache provides generic interface to specialized caching subsystems (for example EhCache, Redis....). In EhCache you can configure things like:
Upvotes: 2
Reputation: 2763
Spring caching is more generic, flexible, and more complex mechanism than a simple getter. It allows to use different cache providers, configure cache invalidation... and many more.
If for your needs it is enough to have just a getter - you should use a getter, to keep your application as simple as possible.
Upvotes: 2