Speed up PHPstorm: cache into a RAM disk (Mac)

With macOs, PHPStorm indexes all your project files and write cache files into files under

/Users/<yourAccount>/Library/Caches/PhpStorm<version>

Thousands of files are stored there. If you want your Mac to use less your hard disk (to extend its life), and also be faster in general, you can create a RAM disk (a virtual disk that uses RAM instead of your disk), and symlink the cache directory there.

I’ve been using this for a while and it seems to work well. The only downside is losing the cache when you restart the IDE, but that takes only a few mins to recreate (and clean automatically), so I’m overall happy with this approach.

Instructions

Add this script to your ~/.bash_profile

The bash command ramDiskCreate will now create a volume called RAMDiskwith a phpstorm-cache inside it. You need to call this script each time you restart and you need to launch PHPStorm (I don’t often restart, so I didn’t bother to make it automatically launched at start).

The thing left to do (only once) is symlinking the cache directory into the newly created directory (update and test with your path, don’t just copy and paste):

mv ~/Library/Caches/PhpStorm2017.3  /Volumes/RAMDisk/phpstorm-cache
ln -s  /Volumes/RAMDisk/phpstorm-cache ~/Library/Caches/PhpStorm2017.3

How to fix docker slowness (volumee mounting) with Docker-sync + PHPStorm file watchers

I’ve experienced lots of slowness using docker for Mac (xhyve), due to volume mounting.

A solution to that I’ve been using successfully for a few week is composed by docker-sync (rsync) and PHPStorm file watcher.

docker-sync (rsync)

I’ve used the rsync solution for docker-sync. Simple, but the downside is that newly created files in the container are not shared outside. That’s not generally a problem except when some code is generated from inside the container (e.g. Doctrine migrations). When needed, a normal mounting for those directories can still be used, or (more complicated), the file can be copied into the host with a docker cp command.

Other downsides are the slowness to rsync watch (a few seconds that can be annoying in some cases), that leads me to the next point:

PHPStorm file watcher

PHPStorm supports the execution of custom scripts on save (file watchers). The idea is having those file watchers to perform an immediate docker cp inside the container with the modified file.

The screenshot should be clear enough. Replace docker bin with the real path of your docker executable (not the symlink), and <CONTAINER_NAME> with your docker container, and adjust /app/ to have docker cp copying files where your docker container expects.

Depending on your needs, you might want to adjust your shares, and decide whether vendor directory needs to be watched or not.

Note: You can’t just use the file watcher, or you’ll have the rebuild (or manually copy all the files) any time you switch branch.

PHPStorm File Watcher Docker cp

PHPStorm File Watcher to copy files inside docker container – example