[ActiveSypport::Cache::FileStore] Add method to delete temporary files and empty directories

ActiveSypport::Cache::FileStore can leave temporary files inside a cache directory. It uses File.atomic_write (link to code). And File.atomic_write uses Tempfile class (link to code) and creates temporary files to check file permissions. So in the case of process crash (e.g. kill -9) there is a possibility that these files will leave there and will never be deleted (and it occurs on my work).

My proposition - 1) create a special method that will find all these files and will remove them or 2) use existed method ActiveSupport::Cache::FileStore#cleanup to remove these temporary files. As a side effect it also can remove empty directories.

WDYT? I will create a PR if you agree that this feature is useful.

When will this method run? At write time? start up time? And more importantly, how would you know which files to remove? I didn’t look at the code in too much detail, but it looks to me that it is using the cache key name as filename, so heavily user application dependent, so you could find yourself removing files that are part of an ongoing cache write.

I don’t know what your use case is, but you could also set the cache directory to something where you run the cleanup yourself, or, possibly, pass it to the kernel’s own /tmp, which might remove it on reboot or if past a certain date (depends on the distribution).

I could see this this running in Rails on a separate thread and probing the file’s mtime before removing, but I am not sure the complexity is warranted.