Class: ActionView::Resolver::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/template/resolver.rb

Overview

Threadsafe template cache

Defined Under Namespace

Classes: SmallCache

Constant Summary collapse

PARTIAL_BLOCK =

preallocate all the default blocks for performance/memory consumption reasons

lambda { |cache, partial| cache[partial] = SmallCache.new }
PREFIX_BLOCK =
lambda { |cache, prefix|  cache[prefix]  = SmallCache.new(&PARTIAL_BLOCK) }
NAME_BLOCK =
lambda { |cache, name|    cache[name]    = SmallCache.new(&PREFIX_BLOCK) }
KEY_BLOCK =
lambda { |cache, key|     cache[key]     = SmallCache.new(&NAME_BLOCK) }
NO_TEMPLATES =

usually a majority of template look ups return nothing, use this canonical preallocated array to save memory

[].freeze

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



53
54
55
56
# File 'lib/action_view/template/resolver.rb', line 53

def initialize
  @data = SmallCache.new(&KEY_BLOCK)
  @query_cache = SmallCache.new
end

Instance Method Details

#cache(key, name, prefix, partial, locals) ⇒ Object

Cache the templates returned by the block



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/action_view/template/resolver.rb', line 63

def cache(key, name, prefix, partial, locals)
  if Resolver.caching?
    @data[key][name][prefix][partial][locals] ||= canonical_no_templates(yield)
  else
    fresh_templates  = yield
    cached_templates = @data[key][name][prefix][partial][locals]

    if templates_have_changed?(cached_templates, fresh_templates)
      @data[key][name][prefix][partial][locals] = canonical_no_templates(fresh_templates)
    else
      cached_templates || NO_TEMPLATES
    end
  end
end

#cache_query(query) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
# File 'lib/action_view/template/resolver.rb', line 78

def cache_query(query) # :nodoc:
  if Resolver.caching?
    @query_cache[query] ||= canonical_no_templates(yield)
  else
    yield
  end
end

#clearObject



86
87
88
89
# File 'lib/action_view/template/resolver.rb', line 86

def clear
  @data.clear
  @query_cache.clear
end

#inspectObject



58
59
60
# File 'lib/action_view/template/resolver.rb', line 58

def inspect
  "#<#{self.class.name}:0x#{(object_id << 1).to_s(16)} keys=#{@data.size} queries=#{@query_cache.size}>"
end

#sizeObject

Get the cache size. Do not call this method. This method is not guaranteed to be here ever.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/action_view/template/resolver.rb', line 93

def size # :nodoc:
  size = 0
  @data.each_value do |v1|
    v1.each_value do |v2|
      v2.each_value do |v3|
        v3.each_value do |v4|
          size += v4.size
        end
      end
    end
  end

  size + @query_cache.size
end