Caching Your API Requests (JSON) In Python Is A Major Optimization

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии • 18

  • @julianunes2124
    @julianunes2124 2 года назад +1

    Hey there, this video helped me a lot today. Thanks! :)

  • @ricgondo
    @ricgondo Год назад

    Wow, I wish I knew this some time ago. Cool!

  • @saifshaikh5559
    @saifshaikh5559 4 месяца назад

    Why I am not able cached google maps api requests?

  • @starogre
    @starogre Год назад

    what data wouldnt change from json exactly? maybe like news articles? or something that never gets updated? any other examples? movie names?

    • @EnzoSantos
      @EnzoSantos Год назад

      Basically, something that changes once in a blue moon. For example, I was using the ytmusicapi in Python these days, which returns metadata about songs, albums, and playlists from RUclips Music. I know it's very unlikely the "The Dark Side of The Moon" album to change their duration or song order between one request and another, so we can cache it. Another example is a Wikipedia page from a dead person or the script of a movie.

  • @20SideDieTriva
    @20SideDieTriva Год назад +2

    have yiu tried pythons requests_cache? it's very robust

    • @Indently
      @Indently  Год назад +1

      Not at all, thanks for the recommendation, I'll check it out!

    • @silkogelman
      @silkogelman Год назад +2

      @@Indently requests_cache is really awesome.
      You'll probably almost never apply the method in your video again as with requests_cache your caching system is easier to implement, maintain and has lots of ways to be configured. (and applied: valuable for testing and production environments)
      I used to do it the way you show in your video too, but with requests_cache I never looked back.
      It has great documentation, but there's no detailed video about it yet.
      So there may be an opportunity for your RUclips channel there.

    • @pietart3596
      @pietart3596 Год назад

      @@silkogelman I agree using requests libraries for production environment is very beneficial. For personal projects Indently's method is easier and more optimised. We aren't depending on an entire third party library

  • @borutouzumaki2120
    @borutouzumaki2120 2 года назад

    Hi this tutorial really help, but can we set the cache to be expired after 30minutes or an hours?

    • @Indently
      @Indently  2 года назад +1

      Of course!

    • @borutouzumaki2120
      @borutouzumaki2120 2 года назад +1

      Could uh please tell me how to do that? Because um still new in python

    • @swagatopablo
      @swagatopablo Год назад +1

      @@borutouzumaki2120 Not to rain on the parade here, and it is always commendable when people share educational stuffs to benefit the community. That said...
      It is not a great design choice to cache on your disk, that too with plain text. That not only makes the server stateful, it can even be slower than the network (if you are making request from on-prem network). But regardless, the video explains the concept and implementation of caching quite well.
      To answer your question directly, if you are to follow this, whenever you write the json, along with the relevant api response, you can add an 'expires' key to indicate when it will expire. Can set it 30 minutes (or any time) from the write timestamp, you can use the unix epoch as the simplest representation of a time instant here to serve the purpose. Look up python pendulum library for time operations. At read time, if the current epoch is higher than expires, then you should refresh the json.
      But a far better choice is to use some service like Redis for caching. (You need do a bit of your research on how to use it.) You can also set up redis keys to self destroy after some time for cache eviction.

  • @Sameer_Pattnaik
    @Sameer_Pattnaik 6 месяцев назад

    It doesn't work from my side

  • @disrael2101
    @disrael2101 2 года назад

    Any automation bootcamp/course coming up?

  • @BroganMcShane-h1k
    @BroganMcShane-h1k Год назад

    The structure of your code could be improved, it doesn't read well