r/learnprogramming • • 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?

2 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/SetAndRepeat 28d ago

If your locks cover the whole fetch-and-update sequence for each key, that addresses the race I meant. Just one nuance: asyncio tasks can still interleave at await points, even on a single thread. So asyncio alone doesn't prevent overlapping refreshes, but your locking may already handle that.