Module: Rddd::Presenters::Cacheable

Defined in:
lib/rddd/presenters/cacheable.rb

Overview

View extension, which would make it cacheable. In case you wanna use it be sure to have a CacheStrategy set in the configuration. As soon as you include the module, View is gonna start caching without any timeout.

Here is how you configure your caching with simple strategy:

class InMemoryStrategy
  def get(key)
    @entries[key]
  end

  def set(key, value, options)
    @entries ||= {}
    @entries[key] = value
  end
end

Rddd.configure do |config|
  config.cache_strategy = InMemoryStrategy.new
end

Each time you hit data method, cache is first checked for presence of the data and if none is there build method is called to compose the view, result is updated to the cache and returned.

Usage:

class ProjectsView < Rddd::Presenters::Presenter
  extend Rddd::Presenters::Cacheable

  def build
    Projects.(@id).map { |project| format(project) }
  end

  def format(project)
    {
      :name => project.name,
      :deathline => project.deathline.strftime("%d %B %Y")
    }
  end
end

ProjectsView.new(account_id).data #= [=> ‘Rddd’, :deathline => ‘01 January 2013’]

## Timeout

By default, there is no expiration to the values you store in the cache. This means, value is theoretically stored for infinite time or until some purging mechanism clean the code from cache as the less queried one.

If you want to have more control of when the certain key should be removed you can use :timeout option. Its configurable per view.

class ProjectsView < Rddd::Presenters::Presenter
  extend Rddd::Presenters::Cacheable

   cache :timeout => 60 * 24
 end

Above call to “‘cache“` with timeout changed its behavior. Each key for a given view now expires in 24 * 60 minutes, therefore in a day from its creation.

## Serialization

By default cache does no serialization so in general String values are expected as caches almost exclusively work with Strings. You can inject serializor with cache configuration.

class ProjectsView < Rddd::Presenters::Presenter
  extend Rddd::Presenters::Cacheable

   cache :serializer => Marshal
 end

Above would call “‘Marshal::dump“` each time before write to cache and “`Marshal::load“` each time the value is retrieved from it. Yo can inject your custom serializor class. Make sure it implements the same interface as Ruby standard “`Marshal::dump“` and “`Marshal::load“`.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



89
90
91
# File 'lib/rddd/presenters/cacheable.rb', line 89

def self.extended(base)
  base.class.extend CacheableConfig
end

.included(base) ⇒ Object



85
86
87
# File 'lib/rddd/presenters/cacheable.rb', line 85

def self.included(base)
  base.extend CacheableConfig
end

Instance Method Details

#dataObject



108
109
110
# File 'lib/rddd/presenters/cacheable.rb', line 108

def data
  __read__ || __update__(build)
end

#invalidateObject

Invalidate the given view so next time its requested no cached value would be given.



97
98
99
# File 'lib/rddd/presenters/cacheable.rb', line 97

def invalidate
  __cache__.delete
end

#warm_upObject

Trigger the warming up of the cache for a given view.



104
105
106
# File 'lib/rddd/presenters/cacheable.rb', line 104

def warm_up
  __update__(build)
end