Using singleton pattern at a request level basis

I am fetching a response twice in an api flow. So how can I use singleton pattern here to return the same response again without making the second call.
17 Replies
JavaBot
JavaBot4w ago
This post has been reserved for your question.
Hey @lars! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan4w ago
What?
lars
larsOP4w ago
Can you elaborate please?
Kyo-chan
Kyo-chan4w ago
Yes. "What" usually mean "That doesn't make a lot of sense to me. Can you elaborate please?"
lars
larsOP4w ago
In the api flow if there is some condition met then I am making an api call to another service to fetch response. Now in the same flow later if some other condition is met then I have to make that call again and fetch essentially the same response that I got the first time. What I want is a way to save this response the first time in some variable and in the second call if this is not null then I return this else make the api call again. Is this clear enough or do you need me to provide some code example for this?
tjoener
tjoener4w ago
Just stick it in a variable and pass it the second time No need for singletons Singletons are jvm wide instances of a class That's not what you need here
lars
larsOP4w ago
That would require me to pass the same response across my codebase to various functions. Is there a way that the class that contains the function that I am calling to fetch the response handles this rather than handling it across the codebase.
tjoener
tjoener4w ago
In classic spring boot, I'd have a service that calls an api, which would be a separate "service" that only deals with the api calls And I'd hold on to the first response and pass it back to the API This is essentially a performance optimization, which a lot of the times, implies having to modify some methods because the abstraction you have now does not allow for this
lars
larsOP4w ago
But what would happen to it when another request comes that fetches record for another "id". Won't it just return the response for a previous request if it was saved.
sooluckyseven
sooluckyseven4w ago
yo, cache your responce by id use something like varnish then cache for 1 min request by id what you need is a http cache do not do that in the code or if you reallyu want it in the code make a timeout hashmap where the key is the id, or use a external cache
tjoener
tjoener4w ago
Oh you're the server ... Yeah either cache it or just redo the request. Shouldn't take long anyway
lars
larsOP4w ago
Yes
tjoener
tjoener4w ago
Maybe not put "fetching" in your question then haha
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
lars
larsOP4w ago
Can't a server also fetch some data from another server?
tjoener
tjoener4w ago
ofc it can, but you said fetching, then talking about you being the server and caching the response pick one
JavaBot
JavaBot4w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server