Cache Eloquent Query Results to Load Pages Instantly

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 2024

ความคิดเห็น • 77

  • @alexeykhachatryan1214
    @alexeykhachatryan1214 4 ปีที่แล้ว +41

    With DB we have two actions - ADD and GET (INSERT and SELECT)
    In case if it's GET - get from cache.
    In case if it's ADD - add in DB, and fire an event to update the cache.
    You could talk about this method in your video.
    Great job!

    • @stefanbogdanovic590
      @stefanbogdanovic590 4 ปีที่แล้ว +5

      Awesome idea! Or just invalidate the cache after inserting new entity. But I think your idea is better :)

    • @impulserr
      @impulserr 4 ปีที่แล้ว +1

      Great idea so you don't need to care about how long to store, you can set forever and just update ;)

    • @eboubaker3722
      @eboubaker3722 3 ปีที่แล้ว

      You just answered the question I asked in my head after watching the video, this is an effective and brilliant technique.

    • @VictorSBFI
      @VictorSBFI 2 ปีที่แล้ว +1

      An Observer should do that work.

    • @alexeykhachatryan1214
      @alexeykhachatryan1214 2 ปีที่แล้ว

      @@VictorSBFI should or can do that?

  • @paulfontaine7819
    @paulfontaine7819 3 ปีที่แล้ว +5

    In case you want the cache to refresh every day, first call after midnight, just add the day in the cache key string. The key then changes every calender day. The old values will be cleared from the cache based on the provided timeout.

  • @raminjavi161
    @raminjavi161 2 ปีที่แล้ว

    You are such a perfect teacher. I love the way you teach; Your lessons are short, real, and useful. Thank you

  • @howtotech3956
    @howtotech3956 4 ปีที่แล้ว +5

    321 likes and 0 dislikes. thats what called a quality channel.

  • @robiparvez
    @robiparvez 4 ปีที่แล้ว +8

    regarding database update issue, you can just add a check, where you can assign the cache value to a variable and add empty check. If empty, query from db else load the cache.

    • @Frank-ou2nr
      @Frank-ou2nr 4 ปีที่แล้ว +2

      He was talking about the use-case where your cache does have data inside of it, but in the meantime your database was updated and the (now old) cache data is still stored in the file. Using what you suggest (checking if the cache has value) will not work in this case; the cache is not empty and thus your query will never execute and the old data will still be used by your application.

    • @oleoni1437
      @oleoni1437 4 ปีที่แล้ว +1

      @@Frank-ou2nr what you mentioned is correct so one way to avoid that it would be to clear the cache every time the data get updated

    • @MrAhmedUA
      @MrAhmedUA 4 ปีที่แล้ว +1

      @@oleoni1437 yeah with some drivers which support cache tags you can for example if you have a cron that gets data from api daily you can do something like
      Cache::tags(['live-system'])->flush();
      also maybe with laravel Eloquent Model upadating ~ deleting ~ creating events you can flush the cache too

  • @alnahian2003
    @alnahian2003 ปีที่แล้ว

    This video is sooo interesting that I frequently comeback rewatch it.

  • @videomania1635
    @videomania1635 3 ปีที่แล้ว

    your each and every video is great source of learning. Thanks...

  • @muhammadfatur8415
    @muhammadfatur8415 4 ปีที่แล้ว

    Very helpfull for me as begineer, thank's sir

  • @mohidden
    @mohidden 4 ปีที่แล้ว

    best of the best laravel tutos.sup of laravel thanks sir.

  • @Shez-dc3fn
    @Shez-dc3fn 4 ปีที่แล้ว +2

    the thing with caching i find is then i have to make sure that the cache is deleted on update or w/e... this can get messy if you have lots of cache for different things..
    cache packages help but no package afaik has solved the problem of caching relation and then deleting them if relation data changes..

    • @educat693
      @educat693 3 ปีที่แล้ว

      I think we can use eloquent model events to clear the related cache. It will be a one time effort for each of the model, after that it will be peaceful life. What is your opinion on that? Please let me know.

  • @devanggandhi3266
    @devanggandhi3266 3 ปีที่แล้ว +1

    Hello Sir,
    I need your suggestion. I am mainly working on API. so is it good to use cache in every api to speed up response ? what are the cons of using the cache ? if my data will update after 2 hours then can it handel it means does cache give the updated data also ?

  • @alexrusin
    @alexrusin 4 ปีที่แล้ว +1

    Nice video. What if a query runs not 1 second, but 10 seconds. Every day there will be a user that will have to take it for the team. That person may not be very happy. Any cache warming strategies?

    • @PovilasKorop
      @PovilasKorop 4 ปีที่แล้ว +5

      Hmm, good point. What about artisan command which runs once per night and refreshes the cache in the background? So no actual user would feel that 10 seconds.

  • @eid9552
    @eid9552 4 ปีที่แล้ว +2

    Thank you for your nice tips.
    I have a question. What if the database is updated before the cache expires? Is there a way you suggest to get the latest data without loosing the performance?

    • @jesteriruka4215
      @jesteriruka4215 3 ปีที่แล้ว

      You can run that query in parallel using crontab and artisan commands.

  • @lifestyleblog6318
    @lifestyleblog6318 2 ปีที่แล้ว +1

    Did you used any extension for checking the Queries

  • @khizer3528
    @khizer3528 2 ปีที่แล้ว

    Thanks for the useful information .

  • @DBRSAstonMartin
    @DBRSAstonMartin 2 ปีที่แล้ว

    How would you refresh the cache say that book title was updated in the database?

  • @apairca
    @apairca 4 ปีที่แล้ว +4

    I think it's much better and recommended to use a Carbon instance instead of seconds for cache time to live.

    • @doremidon
      @doremidon 3 ปีที่แล้ว +1

      Why? Do you can explain?

    • @Maniac-007
      @Maniac-007 10 หลายเดือนก่อน

      @@doremidonreadability

  • @ChangeYourLifeForever
    @ChangeYourLifeForever ปีที่แล้ว

    THANK YOU GODFATHER

  • @ilmammourtada4538
    @ilmammourtada4538 4 ปีที่แล้ว +1

    Thank you for the video. May I ask what is that console you are using to show the query?

    • @PovilasKorop
      @PovilasKorop 4 ปีที่แล้ว +1

      Laravel Debugbar, if that's what you meant by "console".

  • @НиколайШироков-т7л
    @НиколайШироков-т7л 4 ปีที่แล้ว +2

    Thank you for your effort!

  • @Lightnings123
    @Lightnings123 3 ปีที่แล้ว

    is it possible to cache on a resource::collection with pagination?

  • @RpExclusive
    @RpExclusive 3 ปีที่แล้ว +1

    may i know what is the browser extensions you used ?

    • @WireDroid
      @WireDroid 3 ปีที่แล้ว +1

      Laravel debugbar

  • @ihtishamahmad65
    @ihtishamahmad65 2 ปีที่แล้ว

    Where does this cache save? in Browser or our Laravel Application?

  • @santysasa12
    @santysasa12 2 ปีที่แล้ว

    Awesome tip! Thanks a lot

  • @gado_official
    @gado_official 4 ปีที่แล้ว

    I wonder if using pagination is it method would works or not?

  • @HosamHasanMonir
    @HosamHasanMonir 4 ปีที่แล้ว

    Very helpful , Thanks a lot.

  • @namazbekbekarysuly9112
    @namazbekbekarysuly9112 4 ปีที่แล้ว

    It is great video. Let's say one applications needs to cache queries in multiple places.
    How can we regularly trigger caching in all places at particular time.
    Should we make a cron job that runs some command CacheApplicationCommand?

    • @Netherlands031
      @Netherlands031 4 ปีที่แล้ว

      I think you can indeed make a laravel scheduled task to your custom command, that uses Cache::put to make sure there's always a fresh cache and users never have to hit the db (for some queries), and then run that command as well when something in the db gets updated and you need to refresh the cache

  • @lukmanulhakim8841
    @lukmanulhakim8841 3 ปีที่แล้ว

    Are it's save for used on production sir?

  • @javerleo
    @javerleo 2 ปีที่แล้ว

    Excellent explanation! Thanks. Does it work with Eloquent models in Livewire components?

  • @mreduar
    @mreduar 4 ปีที่แล้ว

    I have a website that updates the database every time the user downloads a document, that is, I keep track of the downloads of that document.
    When the database is updated, the cache is lost in the whole app only because of that number update.
    Any suggestion for this situation?

    • @PovilasKorop
      @PovilasKorop 4 ปีที่แล้ว +2

      You're probably caching the wrong thing, then. Instead of having one cache query for the whole app, maybe you should cache some specific things that wouldn't be re-cached after one number update. Or, maybe, you should be ok with holding the cache for a little more and not refresh it after EVERY download, just maybe once per hour or so.

  • @santiagotv6095
    @santiagotv6095 3 ปีที่แล้ว

    @Laravel Daily what is the extension yo used to view the query, timeline and etc.
    like 0:20

    • @WireDroid
      @WireDroid 3 ปีที่แล้ว

      Laravel debugbar

  • @oscarfelipeporras9973
    @oscarfelipeporras9973 4 ปีที่แล้ว

    amazing as always

  • @SKIDKOVOZ
    @SKIDKOVOZ 2 ปีที่แล้ว

    Hi I am from Ukraine. I have a problem, I make link shortener on laravel - with counter by clicks on short link, and i have a trouble with add link to cashe and counter clicks on it - correctly, may I ask about some lesson from you about this, or maybe paid lessons so that you help me learn how to do what I want? I am not programmer i try make some functions for myself.

  • @MzN...
    @MzN... 3 หลายเดือนก่อน

    Thx

  • @amitleuva8739
    @amitleuva8739 2 ปีที่แล้ว

    can a query be cached using redis?

  • @lifestyleblog6318
    @lifestyleblog6318 2 ปีที่แล้ว

    can i used file cached in laravel ?

  • @music-masala-7789
    @music-masala-7789 3 ปีที่แล้ว

    if we have dynamic user Id and other field is user IP , this cache is usefull or not ?

  • @anasbousrateh9537
    @anasbousrateh9537 4 ปีที่แล้ว

    Thank you for this amazing video, it's really helpful

  • @danorex9100
    @danorex9100 4 ปีที่แล้ว

    Thank you for video :)

  • @tienphungkhac1491
    @tienphungkhac1491 ปีที่แล้ว

    What is the extension you just using in the browser?

  • @fire23hummer
    @fire23hummer ปีที่แล้ว

    Perfectooo :)

  • @mukhtorov
    @mukhtorov 4 ปีที่แล้ว

    Thanks!!

  • @isaaclovera8975
    @isaaclovera8975 3 ปีที่แล้ว

    How to the cache in LiteSpeed?

  • @EugeneChe-81
    @EugeneChe-81 4 ปีที่แล้ว +2

    Круто) Спасибо)

  • @fahrim2729
    @fahrim2729 4 ปีที่แล้ว

    Best sir

  • @ricko13
    @ricko13 3 ปีที่แล้ว +1

    This is an excelent feature if you know how and when to use it, I did it wrong and I receive an email from my server provider telling me that my VPS reached a high I/O and so a shutdown was required
    It was triggered cuz I was using the "file" cache driver in Laravel #LessonLearned

  • @adriannvalera9923
    @adriannvalera9923 4 ปีที่แล้ว

    Hi sir. May I know what database you use?

  • @shuvoahmed5217
    @shuvoahmed5217 3 ปีที่แล้ว

    Make a video about laravel horizon with php redis. Its not working on windows

  • @kid_rz
    @kid_rz 4 ปีที่แล้ว

    what aboout paginate data?

    • @mudzarao
      @mudzarao 3 ปีที่แล้ว

      Same question. Have you tried playing around it? If yes, did you manage to find a solution for it?

  • @DanZL1
    @DanZL1 4 ปีที่แล้ว

    Thank you for the great videos you make. Since you are knowledgeable on Laravel, perhaps you can share your perspective on the following. While speaking to a developer about Laravel and converting our WP website to Laravel, their reply was that Laravel is still unstable and has releases every months or so. What's your response to this?

    • @PovilasKorop
      @PovilasKorop 4 ปีที่แล้ว

      I already replied to you, on another video, read the comment here: th-cam.com/video/sAjLREMr-9k/w-d-xo.html&lc=UgxfpVnVL60BIdxtnd54AaABAg