Module: Ramaze::Helper::Cache
- Defined in:
- lib/ramaze/helper/cache.rb
Overview
Caching of simple objects and whole action responses.
Defined Under Namespace
Modules: SingletonMethods
Class Method Summary collapse
-
.included(into) ⇒ Object
Setup needed traits, add the singleton methods and add the caches used by this helper.
Instance Method Summary collapse
-
#cache_value ⇒ Object
The cache wrapper assigned for :action_value.
-
#cache_wrap(action) { ... } ⇒ String
The response body.
Class Method Details
.included(into) ⇒ Object
Setup needed traits, add the singleton methods and add the caches used by this helper.
15 16 17 18 19 20 |
# File 'lib/ramaze/helper/cache.rb', line 15 def self.included(into) into.extend(SingletonMethods) into.add_action_wrapper(6.0, :cache_wrap) into.trait[:cache_action] ||= Set.new Ramaze::Cache.add(:action, :action_value) end |
Instance Method Details
#cache_value ⇒ Object
Returns The cache wrapper assigned for :action_value.
64 65 66 |
# File 'lib/ramaze/helper/cache.rb', line 64 def cache_value Ramaze::Cache.action_value end |
#cache_wrap(action) { ... } ⇒ String
Returns the response body.
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 |
# File 'lib/ramaze/helper/cache.rb', line 27 def cache_wrap(action) cache = Innate::Cache.action ancestral_trait[:cache_action].each do |cache_action| temp = cache_action.dup block = temp.delete(:key) ttl = temp.delete(:ttl) if temp.all?{|key, value| action[key] == value } cache_key = action.full_path cache_key << "_#{action.instance.instance_eval(&block).to_s}" if block if cached = cache[cache_key] action.[:content_type] = cached[:type] else cached = { :body => catch(:respond) { yield }, :type => response['Content-Type'] } if ttl cache.store(cache_key, cached, :ttl => ttl) else cache.store(cache_key, cached) end end return cached[:body] end end yield end |