Simple expiring caching for Ruby on Rails
|Ruby on Rails has some great caching support built right in, but it's most useful when you have MemCacheD or DRb around to serve as a cache store. I don't have access to those everywhere - Dreamhost and other shared hosting providers often prohibit running your own MemCacheD. There's a default memory store, but it won't share cached info between Rails server processes. That leaves the file store, which just writes cached objects to a file that gets shared between all your Rails processes on the same box. The main problem with the file store is that it doesn't support time-based expiration - you have to set up a cron job to sweep out the cache files every once in a while to invalidate your cache.
To get around this, I wrote a tiny, obvious little module that gives a simple syntax for caching objects with the file store. The trick is that it stores the insert time whenever it caches a new object, and compares it with the current time every time it looks it up. If the cached object is...
more