skip to Main Content

This is my first time dealing with caching, and even though I looked through the laravel docs and other various sites for instructions of how to set it up, I’m still at a bit of a loss as which one to use and what the different cache drivers do.

My current scenario is that I have a scheduling system where you can create pdfs of the current week of classes. They can also choose a date in the future and make a pdf of that week as well. This is a frontend feature, so anyone who visits the site would be able to use it. There are many classes and variations of patterns that the classes can have, so the query would have a lot of records to look through. Which driver would be best out of the supported cache drivers?? (apc, array, database, file, memcached & redis)

Brownie Points

I’d like to get an understanding of which to use and why so I can make the best decisions for future projects. So what does each do/when would it be best to use them?? — Doesn’t need to be answered to get accepted answer, but I’d really like to know.

Thanks!

2

Answers


  1. Typically you would use cache for frequent queries (when you need to perform a particular read op frequently but write not as frequently). If this isn’t the case, you would generally fallback to DB.

    Looking at your use case, it sounds like it’s a batch job that would run once a week. So, it’s an infrequent task and the data would be fresh every week. So what exactly do you hope to achieve by caching?

    Login or Signup to reply.
  2. When it comes to using cache in Laravel you have 3 possible “families” that you should concider:

    1. Temporary/Debug

      • array
    2. Always available

      • file
      • database
      • apc (I would not trust this one since PHP7)
    3. Dedicated

      • redis
      • memcached

    Since you can easily replace the cache drivers you dont need to pick one based on your use case, but more based on your server needs/load and possibilities.

    For example on your development machine I suggest using file, since this way you wont need any extra software clogging your PC plus you gain the ability to quickily clear the cache even if you do something really bad like breaking the artisan command. All you need to do is delete the storage/framework folder and you have a fresh instance again (make sure to regenerate the .gitignore files from your repository after that)

    For your main server you have to think about your possibilities. If you have one of those free hosting websites you almost certainly won’t be able to install any new software, so you can consider using file or database.
    Even though database will probably be faster than file, it is in most cases the weakest point of your website, and trying to push even more data into that bottleneck is not a good idea, that is why I would suggest against using it, and instead stick to files.

    If you have a dedicated server, than you should definately pick memcached or redis. Which one of the two? It depends on many factors, and you can find a lot of comparations online, just look for one. I personally prefer redis becouse of its ability to persist data, but either one is a good solution.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search