Commit 4688bc9cc97ed6caa3035edc94f0b0de0ed47a5b
1 parent
6d5099fc
Exists in
master
and in
29 other branches
ActionItem1016: store timeouts in the cache itself
Showing
1 changed file
with
31 additions
and
8 deletions
Show diff stats
vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb
... | ... | @@ -2,30 +2,53 @@ |
2 | 2 | module ActionController |
3 | 3 | module Cache |
4 | 4 | module TimedCache |
5 | - #used to store the associated timeout time of cache key | |
6 | - @@cache_timeout_values = {} | |
7 | 5 | #handles standard ERB fragments used in RHTML |
8 | 6 | def cache_timeout(name={}, expire = 10.minutes.from_now, &block) |
9 | 7 | unless perform_caching then block.call; return end |
10 | 8 | key = fragment_cache_key(name) |
11 | 9 | if is_cache_expired?(key,true) |
12 | 10 | expire_timeout_fragment(key) |
13 | - @@cache_timeout_values[key] = expire | |
11 | + set_timeout(key, expire) | |
14 | 12 | end |
15 | 13 | cache_erb_fragment(block,name) |
16 | 14 | end |
17 | 15 | #handles the expiration of timeout fragment |
18 | 16 | def expire_timeout_fragment(key) |
19 | - @@cache_timeout_values.keys.select{|k| key === k}.each do |k| | |
20 | - @@cache_timeout_values[k] = nil | |
21 | - end | |
22 | - expire_fragment(/#{key}/) | |
17 | + delete_timeout(key) | |
18 | + expire_fragment(key) | |
23 | 19 | end |
24 | 20 | #checks to see if a cache has fully expired |
25 | 21 | def is_cache_expired?(name, is_key = false) |
26 | 22 | key = is_key ? name : fragment_cache_key(name) |
27 | - return (!@@cache_timeout_values[key]) || (@@cache_timeout_values[key] < Time.now) | |
23 | + timeout = get_timeout(key) | |
24 | + return (!timeout) || (timeout < Time.now) | |
25 | + end | |
26 | + | |
27 | + # from http://code.google.com/p/timedcachedfragment/issues/detail?id=1 | |
28 | + def cache_erb_fragment(block, name = {}, options = nil) | |
29 | + unless perform_caching then block.call; return end | |
30 | + | |
31 | + buffer = eval(ActionView::Base.erb_variable, block.binding) | |
32 | + | |
33 | + if cache = read_fragment(name, options) | |
34 | + buffer.concat(cache) | |
35 | + else | |
36 | + pos = buffer.length | |
37 | + block.call | |
38 | + write_fragment(name, buffer[pos..-1], options) | |
39 | + end | |
40 | + end | |
41 | + | |
42 | + def delete_timeout(key) | |
43 | + expire_fragment('timeout:' + key) | |
28 | 44 | end |
45 | + def get_timeout(key) | |
46 | + read_fragment('timeout:' + key) | |
47 | + end | |
48 | + def set_timeout(key, value) | |
49 | + write_fragment('timeout:' + key, value) | |
50 | + end | |
51 | + | |
29 | 52 | end |
30 | 53 | end |
31 | 54 | end | ... | ... |