Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Which cache driver to use?

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!

like image 872
Sensoray Avatar asked Aug 14 '17 15:08

Sensoray


People also ask

What are cache drivers?

The Cache Manager is the server that directs all of the cache locations to download drivers and other print-related items.

Where Laravel cache is stored?

Laravel provides a unified API for various caching systems. The cache configuration is located at app/config/cache. php . In this file you may specify which cache driver you would like used by default throughout your application.

How do I enable caching in Laravel?

To enable Laravel caching services, first use the Illuminate\Contracts\Cache\Factory and Illuminate\Contracts\Cache\Repository, as they provide access to the Laravel caching services. The Factory contract gives access to all the cache drivers of your application.

What is Redis cache in Laravel?

Laravel supports the use of Redis, which uses caches for temporary data storage to speed up the process of performing database queries and getting feedback, which will, in turn, reduce the amount of time spent pulling up data.


1 Answers

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.

like image 78
HubertNNN Avatar answered Oct 13 '22 06:10

HubertNNN