SafeCache ========= SafeCache is a plugin to DRY up the begin/rescue wrappers around your cache access code. Typically, you wrap your cache access code like this: begin recent_books = Rails.cache.read("books-recent", Book.recent) rescue recent_books = Book.recent Rails.cache.write("books-recent", recent_books) end You do this to ensure that, in the event that the cache server is unavailable, your code will not raise an exception but instead degrade gracefully by falling back to accessing the database. SafeCache allows you to shorten the code above to: Book.safe_cache(:recent) The other advantage of this to memcached plugins like cache_fu is it isn't tied to memcached. You can use redis, MongoDB, or any other cache storage that can be used by Rails. Installation ======= script/plugin install git://github.com/radamanthus/safe_cache.git In your ActiveRecord model: class Book < ActiveRecord::Base caches_safely end Example Usage ======= On class methods: top_books = Book.cache_safely(:top, {:limit => 5}) On instance methods: last_book = Book.last last_book_authors = last_book.cache_safely(:authors) How are cache keys computed? History ======= SafeCache started as a module built for Devex.com to DRY up memcache code. In extracting the plugin, I changed the code to use Rails.cache instead of the direct memcached calls. This way it can support other cache storage mechanisms, like redis or MongoDB. Copyright (c) 2010 Radamanthus Batnag, released under the MIT license
radamanthus/safe_cache
SafeCache is a plugin to DRY up the begin/rescue wrappers around your cache access code.
RubyMIT