r/learnprogramming • u/nullrevolt • 28d ago
Design Patterns
I'm a 5+ year developer who never really studied programming patterns and paradigms. I'm currently tutoring a student in a high-school and I got to thinking about my own programming and design patterns.
When it comes to building an application, in my case one that makes requests, caches the data, and performs some analysis, I'm often considering where it makes the most sense to bind certain functionality and call it from. I'm primarily using an object-oriented paradigm.
Currently I have several threads responsible for calling a number of asynchronous tasks that should:
- Make a web request (retrieving data from an API)
- Potentially make follow up requests
- Cache the response data
- Analyze/Display the most recently cached data
Right now, I have the cache responsible for calling out to the API so that it can retrieve its data to store, but I'm questioning this from an architectural standpoint. It almost seems to me as if the thread or task should have a series of statements more akin to functional programming to control flow.
How would you approach this or recommend a design?
View all comments
1
u/SetAndRepeat 28d ago
A cache that fetches missing data is a valid design: that's read-through caching. Also, a sequence of statements coordinating operations is imperative programming, and fits perfectly well inside an OO method.
For your workflow, I'd have a service coordinate the API requests, including follow-ups, then store the completed result in the cache. The API client handles HTTP, the cache handles storage, and analysis receives a data snapshot. That lets you test the analysis without networking. Your threads/tasks run this workflow; the coordination doesn't need to live inside the cache.
With concurrent refreshes, watch what "latest" means: an older request can finish after a newer one and overwrite its result. Allowing only one refresh per cache key at a time is a simple starting point.