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
⌛
This post has been reserved for your question.
Hey @lars! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
What?
Can you elaborate please?
Yes. "What" usually mean "That doesn't make a lot of sense to me. Can you elaborate please?"
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?
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
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.
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
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.
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
Oh you're the server ...
Yeah either cache it or just redo the request. Shouldn't take long anyway
Yes
Maybe not put "fetching" in your question then haha
💤
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.
Can't a server also fetch some data from another server?
ofc it can, but you said fetching, then talking about you being the server and caching the response
pick one
💤
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.