-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
51 lines (34 loc) · 1.47 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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