Class: Rack::Component::Memoized
- Inherits:
-
Rack::Component
- Object
- Rack::Component
- Rack::Component::Memoized
- Defined in:
- lib/rack/component.rb
Overview
Rack::Component::Memoized is just like Component, only it caches its rendered output in memory and only rerenders when called with new arguments.
Constant Summary collapse
- CACHE_SIZE =
limit to 100 keys by default to prevent leaking RAM
100
Constants inherited from Rack::Component
Class Method Summary collapse
-
.cache ⇒ Rack::Component::ComponentCache
Access or instantiate a class-level cache.
-
.call(*args, &block) ⇒ String, Object
The cached (or computed) output of render.
-
.clear_caches ⇒ Object
Clear the cache of each descendant class.
-
.key(*args) ⇒ Integer
A cache key for this component.
-
.memoized(*args, &miss) ⇒ Object
Check the class-level cache, set it to &miss if nil.
Methods inherited from Rack::Component
Class Method Details
.cache ⇒ Rack::Component::ComponentCache
Access or instantiate a class-level cache
53 54 55 |
# File 'lib/rack/component.rb', line 53 def self.cache @cache ||= ComponentCache.new(const_get(:CACHE_SIZE)) end |
.call(*args, &block) ⇒ String, Object
Returns the cached (or computed) output of render.
82 83 84 |
# File 'lib/rack/component.rb', line 82 def self.call(*args, &block) memoized(*args) { super } end |
.clear_caches ⇒ Object
Clear the cache of each descendant class. Generally you’ll call this on Rack::Component::Memoized directly.
101 102 103 104 105 |
# File 'lib/rack/component.rb', line 101 def self.clear_caches ObjectSpace.each_object(singleton_class) do |descendant| descendant.cache.flush end end |
.key(*args) ⇒ Integer
Returns a cache key for this component.
93 94 95 |
# File 'lib/rack/component.rb', line 93 def self.key(*args) args.hash end |
.memoized(*args, &miss) ⇒ Object
Check the class-level cache, set it to &miss if nil.
88 89 90 |
# File 'lib/rack/component.rb', line 88 def self.memoized(*args, &miss) cache.fetch(key(*args), &miss) end |