Last updated 2020-04-18 14:46:23

Laravel File Driven Cache On Steroids

fast car image by toine G [→]

As of Laravel 7, there are 6 available cache drivers, with APC giving the best results and the file driver being the only one that requires no extra setup.

I was speaking to a friend last night, who mentioned they used Redis as their cache driver. At this point, I have a project that still uses the file driver.

I was thinking I could use some of that sweet performance of memory-driven cache but I really don’t want to install Redis at this time. Right then a solution hit me, something I knew but not really used. “tmpfs”.

01: $ mount -t tmpfs -o size=12m tmpfs storage/framework/cache

What does it do?

desktop memory stick image [→]

Photo by Liam Briese

tmpfs: Allows you to store files in RAM by acting as a directory.

Running the above on a Linux server within your Laravel directory will map your storage/framework/cache to your RAM, which means you enjoy the drop in latency of your cached files by using the RAM as opposed to disk IO.

It's a small improvement that can go a long way especially if you use cache a lot within your app.

You can make sure your server switch to RAM storage on restart by putting the command below in your server's system configuration file /etc/fstab

01: tmpfs storage/framework/cache tmpfs nodev,nosuid,noexec,nodiratime,size=12m 0 0

And to revert back to Disk IO use the command below

01: $ umount storage/framework/cache

The End …

Here is another article you might like 😊 "Laravel PHP 8 throws error Too few arguments to function | Fix"