Class Index [+]

Quicksearch

Ramaze::Helper::Cache

Caching of simple objects and whole action responses.

Public Class Methods

included(into) click to toggle source

Setup needed traits, add the singleton methods and add the caches used by this helper.

@param [Class] into Class that this Module is included into @author manveru

    # File lib/ramaze/helper/cache.rb, line 16
16:       def self.included(into)
17:         into.extend(SingletonMethods)
18:         into.add_action_wrapper(6.0, :cache_wrap)
19:         into.trait[:cache_action] ||= Set.new
20:         Ramaze::Cache.add(:action, :cache_helper_value)
21:       end

Public Instance Methods

cache_value(key = nil, options = {}) click to toggle source

This method is used to access Ramaze::Cache.cache_helper_value. It provides an easy way to cache long-running computations, gathering external resources like RSS feeds or DB queries that are the same for every user of an application. This method changes behaviour if a block is passed, which can be used to do lazy computation of the cached value conveniently when using a custom TTL or longer expressions that don’t fit on one line with ||=.

@example to get the cache object directly

  count = cache_value[:count] ||= Article.count

@example with block

  count = cache_value(:count){ Article.count }
  count = cache_value(:count, :ttl => 60){ Article.count }

@return [Object] The cache wrapper assigned for :cache_helper_value @see Innate::Cache @author manveru

    # File lib/ramaze/helper/cache.rb, line 86
86:       def cache_value(key = nil, options = {})
87:         cache = Ramaze::Cache.cache_helper_value
88: 
89:         if key and block_given?
90:           if found = cache[key]
91:             found
92:           else
93:             cache.store(key, yield, options)
94:           end
95:         else
96:           cache
97:         end
98:       end
cache_wrap(action) click to toggle source

@param [Action] action The currently wrapped action @yield The next block in wrap_action_call @return [String] the response body @see Innate::Node#wrap_action_call @author manveru

    # File lib/ramaze/helper/cache.rb, line 30
30:       def cache_wrap(action)
31:         cache = Innate::Cache.action
32: 
33:         ancestral_trait[:cache_action].each do |cache_action|
34:           temp  = cache_action.dup
35:           block = temp.delete(:key)
36:           ttl   = temp.delete(:ttl)
37: 
38:           if temp.all?{|key, value| action[key] == value }
39:             cache_key = action.full_path
40:             cache_key << "_#{action.instance.instance_eval(&block).to_s}" if block
41: 
42:             if cached = cache[cache_key]
43:               action.options[:content_type] = cached[:type]
44:             else
45:               cached = {
46:                 :body => catch(:respond) { yield },
47:                 :type => response['Content-Type']
48:               }
49: 
50:               if ttl
51:                 cache.store(cache_key, cached, :ttl => ttl)
52:               else
53:                 cache.store(cache_key, cached)
54:               end
55:             end
56: 
57:             return cached[:body]
58:           end
59:         end
60: 
61:         yield
62:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.