I’ve just been starting out with Vertx. I wonder if there’s a way to store/cache a response data for a period of time?
For example, the first time a user calls my API, it will query the database on the server and return a data. I want to save/cache this data to a local file (or memory) on the server for, for example, 3 hours. During these 3 hours, if any other user make another call to the API, it will use the cached data instead. After 3 hours, the cached data reset.
I tried searching on Google for solutions like Vertx Redis, or StaticHandler, but they seem over-complicated, and don’t seem to match my needs?
Is there a simple way to achieve this?
2
Answers
You could use a cache (with some
Map
maybe) andVertx::setTimer
to invalidate it after 3 hours. Assuming you are usingRouter
:You don’t have to reinvent wheel for Vert.x again. There are plenty of tools out there to do caching for you and Google Guava cache is the one that should suite your need very well.
In your verticle you define a
cache
and access your DB though it. The cache does the rest:then whenever you need to get your data: