8

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!

Sensoray
  • 2,360
  • 2
  • 18
  • 27
  • Try to avoid asking questions that don't have a specific technical problem. Currently, you're asking for the differences, which could be closed as too broad, and you're also asking for best use cases, which could be closed as primarily opinion based. It's a valid question, but I don't think you're too likely to get answers that fall into Stackoverflow's on-topic requirement. – Tim Lewis Aug 14 '17 at 16:02
  • I'll make edits so that it will be more specific then, thanks for the heads up. – Sensoray Aug 14 '17 at 17:31
  • No worries. I think your best approach is to try some of these out; test caching of small result sets using each of the drivers and see if you get any issues. I've used the `file` driver with slight issues when it comes to file permissions, but otherwise it works for my uses. – Tim Lewis Aug 14 '17 at 17:38

2 Answers2

20

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.

HubertNNN
  • 1,727
  • 1
  • 14
  • 29
  • Why would you not trust APC? Is it just an issue with PHP 7, or has PHP 8 fixed it? – cyreb7 Mar 24 '23 at 18:37
  • @cyreb7 There was a lot of uncertainty about APC when PHP7 got released. Some mentioned it was discontinued others that it is still supported. To be honest I am not sure what is the current state of APC. – HubertNNN Mar 27 '23 at 09:01
0

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?

Paras
  • 9,258
  • 31
  • 55
  • 1
    It's actually goes on the frontend where many people can look at the classes for the week, or the week of any date they want, and they can make pdfs for whichever week they choose. So in case people start making lots of pdfs, I'd like to cache it. – Sensoray Aug 15 '17 at 12:15