Forum: Ruby on Rails expire entire directory of fragments?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Avram D. (Guest)
on 2008-12-13 05:46
(Received via mailing list)

I am trying to figure out if there is an appropriate and for that
matter, already built-in way to expire an entire directory of cache
fragments. My app has certain situations where an entire subdirectory
in the cache becomes obsolete at once - rather than expiring each file
one at a time, or using a regexp to iterate over the whole thing, it
would be nice to just make the whole directory go *poof*.

I realize I could to this manually, but it *feels* like there is
something evil about not using expire_fragment() to get rid of
something that was made by cache(). For example, I could compute the
dir manually, and call

  system("rm -rf #{dir}"). Any reason not to do that?

And even slicker solution would be

  File::rename(dir, uniq_filename('tmp/old_caches_'))

and then let a cron job "rm -rf tmp/old_caches_*" out-of-band.

Can anyone think of a reason not to do this, or in general a good
reason not to get rid of cache files w/out using expire_fragment?


So, if there was nobody else when God said "Let there be light," was
He talking to Himself? Does that mean He is insane? Because that would
explain a lot
Philip H. (Guest)
on 2008-12-13 07:42
(Received via mailing list)
> dir manually, and call
>   system("rm -rf #{dir}"). Any reason not to do that?

Don't make a call to system. Use the File class instead... rm_rf or
your rename and then remove depending on how big a directory it is.

If you dig through the cache classes file store eventually calls
"File.delete(real_file_path(name))" so what you're doing isn't that
unreasonable, but it also calls it's super method and I haven't bother
to look to see what it does.

Another option would be to create your own cache store, subclassing
off of FileStore and then override the delete method to check if it's
a directory being passed in and if so, wipe the whole thing out,
otherwise just the file.  Or look to see if there is a reason not to
just do that directly in FileStore and submit a ticket to have it
This topic is locked and can not be replied to.