Module: Ramaze::Helper::Cache

Defined in:
lib/ramaze/helper/cache.rb

Overview

This helper is providing easy access to a couple of Caches to use for smaller amounts of data.

Constant Summary collapse

C =
Ramaze::Cache

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Create the Cache.value_cache on inclusion if it doesn’t exist yet.



13
14
15
# File 'lib/ramaze/helper/cache.rb', line 13

def self.included(klass)
  C.add(:value_cache) unless C::CACHES.has_key?(:value_cache)
end

Instance Method Details

#cache(*args) ⇒ Object

Example:

class FooController < Ramaze::Controller
  helper :cache
  cache :index, :map_of_the_internet
end

cache supports these options

[+:ttl+]  time-to-live in seconds
[+:key+]  proc that returns a key to store cache with

Example:

class CacheController < Ramaze::Controller
  helper :cache

  # for each distinct value of request['name']
  # cache rendered output of name action for 60 seconds
  cache :name, :key => lambda{ request['name'] }, :ttl => 60

  def name
    "hi #{request['name']}"
  end
end

cache acts as a wrapper for value_cache if no args are given



44
45
46
47
48
49
50
51
52
# File 'lib/ramaze/helper/cache.rb', line 44

def cache *args
  return value_cache if args.size == 0

  opts = args.last.is_a?(Hash) ? args.pop : {}

  args.compact.each do |arg|
    actions_cached[arg.to_sym] = opts
  end
end