DEV Community

EDDYMENS
EDDYMENS

Posted on • Originally published at Medium on

Laravel file driven cache on steroids

Photo by toine G on Unsplash

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”.

$ mount -t tmpfs -o size=12m tmpfs storage/framework/cache
Enter fullscreen mode Exit fullscreen mode

What does it do?

Photo by Liam Briese on Unsplash

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 switches to RAM storage on restart by putting the command below in your server's system configuration file /etc/fstab

tmpfs storage/framework/cache tmpfs nodev,nosuid,noexec,nodiratime,size=12m 0 0
Enter fullscreen mode Exit fullscreen mode

And to revert back to Disk IO use the command below

$ umount storage/framework/cache
Enter fullscreen mode Exit fullscreen mode

The End …

Top comments (1)

Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

That's pretty clever! If you were using docker, you could automate this as well. This is actually how I run my database during unit tests. I never made the connection in my head to use it for caching. Nice find!