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

Instance Method Summary collapse

Class Method Details

.included(into) ⇒ Object

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

Parameters:

  • into (Class)

    Class that this Module is included into

Author:

  • manveru



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_valueObject

Returns The cache wrapper assigned for :action_value.

Returns:

  • (Object)

    The cache wrapper assigned for :action_value

See Also:

  • Innate::Cache

Author:

  • manveru



51
52
53
# File 'lib/ramaze/helper/cache.rb', line 51

def cache_value
  Ramaze::Cache.action_value
end

#cache_wrap(action) { ... } ⇒ String

Returns the response body.

Parameters:

  • action (Action)

    The currently wrapped action

Yields:

  • The next block in wrap_action_call

Returns:

  • (String)

    the response body

See Also:

  • Innate::Node#wrap_action_call

Author:

  • manveru



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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
    ttl = temp.delete(:ttl)

    if temp.all?{|key, value| action[key] == value }
      if cached = cache[temp]
        return cached
      elsif ttl
        return cache.store(temp, yield, :ttl => ttl)
      else
        return cache.store(temp, yield)
      end
    end
  end

  yield
end

#value_cacheObject

Deprecated.

Use the #cache_value method instead

Author:

  • manveru



58
59
60
61
62
# File 'lib/ramaze/helper/cache.rb', line 58

def value_cache
  Ramaze::deprecated('Innate::Helper::Cache#value_cache',
                     'Innate::Helper::Cache#cache_value')
  cache_value
end