public
Description: Merb More: The Full Stack. Take what you need; leave what you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-more.git
ezmobius (author)
Thu Mar 06 14:29:56 -0800 2008
commit  9efa802c756f57eee432df5205854e415f19d125
tree    f32f1260449a80f2617d7ee00d3e9422ceb9878e
parent  bec1d610ee776927bbbc8d038835eb0b303da6db
merb-more / merb-cache / lib / merb-cache / cache-action.rb
100644 70 lines (62 sloc) 1.808 kb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Merb::Cache
  cattr_accessor :cached_actions
  self.cached_actions = {}
end
 
module Merb::Cache::ControllerClassMethods
  def cache_action(action, from_now = nil)
    cache_actions([action, from_now])
  end
 
  def cache_actions(*actions)
    if actions.any? && Merb::Cache.cached_actions.empty?
      before(:cache_action_before)
      after(:cache_action_after)
    end
    actions.each do |action, from_now|
      _actions = Merb::Cache.cached_actions[controller_name] ||= {}
      _actions[action] = from_now
    end
    true
  end
end
 
module Merb::Cache::ControllerInstanceMethods
  def cached_action?(o)
    key = Merb::Controller._cache.key_for(o, controller_name, true)
    Merb::Controller._cache.store.cached?(key)
  end
 
  def expire_action(o)
    Merb::Controller._cache.expire_key_for(o, controller_name, true) do |key, match|
      if match
        Merb::Controller._cache.store.expire_match(key)
      else
        Merb::Controller._cache.store.expire(key)
      end
    end
    true
  end
 
  private
 
  def _cache_action(data = nil)
    controller = controller_name
    action = action_name.to_sym
    actions = Merb::Controller._cache.cached_actions[controller]
    return unless actions && actions.key?(action)
    path = request.path
    path.chop! if path[-1] == "/"
    path = "index" if path.empty?
    if data
      from_now = Merb::Controller._cache.cached_actions[controller][action]
      Merb::Controller._cache.store.cache_set(path, data, from_now)
    else
      @capture_action = false
      _data = Merb::Controller._cache.store.cache_get(path)
      throw(:halt, _data) unless _data.nil?
      @capture_action = true
    end
    true
  end
 
  def cache_action_before
    _cache_action
  end
  def cache_action_after
    _cache_action(body) if @capture_action
  end
end