Class: RedisTemplateResolver

Inherits:
ActionView::Resolver
  • Object
show all
Defined in:
lib/redis_template_resolver.rb

Overview

Looks for templates in a redis cache, The Rails Way(tm).

The lookup procedure is as follows:

1. Determine current consuming application name. If unknown, use default 
   template.

2. look up template in local cache. 
   If found in local cache, and not expired: Use this template.
   If found in local cache, but expired: delete from cache, continue to 
   next step.
   If not found in local cache: continue to next step.

3. Attempt to retrieve from redis cache.
   If found, write back to local cache, with a lifetime of 
   +local_cache_ttl+ seconds.
   If not found, continue to next step.

4. Attempt to retrieve from remote URL, configured for the consuming 
   application. 
   If successful, write back to redis (indefinitely) and local cache
   with an expiration of +local_cache_ttl+ seconds.
   If not successful, continue to next step.

   Note that there is not mutex for the HTTP retrieval, so ALL rails 
   processes could theoretically perform this step at the same time.
   Fixme: Add Mutex handling with redis.

5. Write default template to local cache with a lifetime of 
   +local_cache_negative_ttl+ seconds to avoid hammering redis and the
   remote server.
   Use the default template.

NOTE: This class has some stuff hard wired into it that makes it (as is)
      only useful to look up mustache templates that will be used for a
      view layout.

Defined Under Namespace

Classes: TemplateRejectedError

Constant Summary collapse

@@local_cache_ttl =

seconds

60
@@local_cache_negative_ttl =

seconds

10
@@http_timeout =

seconds

5
@@template_handler_name =
"erb"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_cacheObject

Clears the local cache completely.



63
64
65
# File 'lib/redis_template_resolver.rb', line 63

def self.clear_cache
  @@cache = {}
end

Instance Method Details

#find_templates(name, prefix, partial, details) ⇒ Object

Called by rails to fetch the template.

Returns an array of ActionView::Template instances. 
If successful, only one template is returned inside the returned array.

Note that to indicate lookup failure, this function MUST return an empty
array!


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redis_template_resolver.rb', line 75

def find_templates( name, prefix, partial, details )
  Rails.logger.debug "Called RedisTemplateResolver"

  return [] unless name.start_with?( "redis:" ) 
  if respond_to?( :resolver_guard, true )
    return [] unless resolver_guard( name, prefix, partial, details )
  end


  _, @template_name = name.split(':', 2 )
  Rails.logger.debug "RedisTemplateResolver fetching template with name: #{@template_name}"

  template_body = fetch_template

  path = ActionView::Resolver::Path.build( name, prefix, nil )
  handler = ActionView::Template.handler_for_extension( self.template_handler_name )

  template = ActionView::Template.new( template_body, path, handler,
                                       :virtual_path => path.virtual,
                                       :format       => [ :html ],
                                       :updated_at   => Time.now )
  return [ template ]
end