Class: Merb::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-cache/cache-action.rb,
lib/merb-cache/merb-cache.rb,
lib/merb-cache/cache-page.rb

Defined Under Namespace

Modules: ControllerClassMethods, ControllerInstanceMethods Classes: StoreNotFound

Constant Summary collapse

DEFAULT_CONFIG =
{
  :cache_html_directory => Merb.dir_for(:public) / "cache",

  #:store => "database",
  #:table_name => "merb_cache",

  #:disable => "development", # disable merb-cache in development
  #:disable => true, # disable merb-cache in all environments

  :store => "file",
  :cache_directory => Merb.root_path("tmp/cache"),

  #:store => "memcache",
  #:host => "127.0.0.1:11211",
  #:namespace => "merb_cache",
  #:track_keys => true,

  #:store => "memory",
  # store could be: file, memcache, memory, database, dummy, ...
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/merb-cache/merb-cache.rb', line 6

def config
  @config
end

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/merb-cache/merb-cache.rb', line 6

def store
  @store
end

Instance Method Details

#expire_key_for(options, controller, controller_based = false) ⇒ Object

Compute a cache key and yield it to the given block It is used by the #expire_page, #expire_action and #expire methods.

Parameters

options<String, Hash>

The key or the Hash that will be used to build the key

controller<String>

The name of the controller

controller_based<Boolean>

only used by action and page caching

Options (options)

:key<String>

The complete or partial key that will be computed.

:action<String>

The action name that will be used to compute the key

:controller<String>

The controller name that will be part of the key

:params<Array>

The params will be joined together (with ‘/’) and added to the key

:match<Boolean, String>

true, it will try to match multiple cache entries string, shortcut for => “mykey”, :match => true

Examples

expire(:key => "root_key", :params => [session[:me], params[:id]])
expire(:match => "root_key")
expire_action(:action => 'list')
expire_page(:action => 'show', :controller => 'news')

Returns

The result of the given block



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/merb-cache/merb-cache.rb', line 81

def expire_key_for(options, controller, controller_based = false)
  key = ""
  if options.is_a? Hash
    case
    when key = options[:key]
    when action = options[:action]
      controller = options[:controller] || controller
      key = "/#{controller}/#{action}"
    when match = options[:match]
      key = match
    end
    if _params = options[:params]
      key += "/" + _params.join("/")
    end
    yield key, !options[:match].nil?
  else
    yield controller_based ? "/#{controller}/#{options}" : options, false
  end
end

#key_for(options, controller, controller_based = false) ⇒ Object

Compute a cache key based on the given parameters Only used by the #cached_page?, #cached_action?, #cached?, #cache, #cache_get and #cache_set methods

Parameters

options<String, Hash>

The key or the Hash that will be used to build the key

controller<String>

The name of the controller

controller_based<Boolean>

only used by action and page caching

Options (options)

:key<String>

The complete or partial key that will be computed.

:action<String>

The action name that will be used to compute the key

:controller<String>

The controller name that will be part of the key

:params<Array>

The params will be joined together (with ‘/’) and added to the key

Examples

cache_set("my_key", @data)
cache_get(:key => "root_key", :params => [session[:me], params[:id]])

Returns

The computed key



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/merb-cache/merb-cache.rb', line 123

def key_for(options, controller, controller_based = false)
  key = ""
  if options.is_a? Hash
    case
    when key = options[:key]
    when action = options[:action]
      controller = options[:controller] || controller
      key = "/#{controller}/#{action}"
    end
    if _params = options[:params]
      key += "/" + _params.join("/")
    end
  else
    key = controller_based ? "/#{controller}/#{options}" : options
  end
  key
end

#startObject

Called in the after_app_loads loop and instantiate the right backend

Raises

Store#NotFound

If the cache_store mentionned in the config is unknown



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/merb-cache/merb-cache.rb', line 40

def start
  @config = DEFAULT_CONFIG.merge(Merb::Plugins.config[:merb_cache] || {})
  if @config[:disable] == true || Merb.environment.to_s == @config[:disable].to_s
    config[:disable_page_caching] = true
    config[:store] = "dummy"
  end
  @config[:cache_html_directory] ||= Merb.dir_for(:public) / "cache"
  require "merb-cache/cache-store/#{@config[:store]}"
  @store = Merb::Cache.const_get("#{@config[:store].capitalize}Store").new
  Merb.logger.info("Using #{@config[:store]} cache")
rescue LoadError
  raise Merb::Cache::StoreNotFound, @config[:store].inspect
end