@@ -49,84 +49,18 @@ def parse(path)
49
49
end
50
50
end
51
51
52
- # Threadsafe template cache
53
- class Cache # :nodoc:
54
- class SmallCache < Concurrent ::Map
55
- def initialize ( options = { } )
56
- super ( options . merge ( initial_capacity : 2 ) )
57
- end
58
- end
59
-
60
- # Preallocate all the default blocks for performance/memory consumption reasons
61
- PARTIAL_BLOCK = lambda { |cache , partial | cache [ partial ] = SmallCache . new }
62
- PREFIX_BLOCK = lambda { |cache , prefix | cache [ prefix ] = SmallCache . new ( &PARTIAL_BLOCK ) }
63
- NAME_BLOCK = lambda { |cache , name | cache [ name ] = SmallCache . new ( &PREFIX_BLOCK ) }
64
- KEY_BLOCK = lambda { |cache , key | cache [ key ] = SmallCache . new ( &NAME_BLOCK ) }
65
-
66
- # Usually a majority of template look ups return nothing, use this canonical preallocated array to save memory
67
- NO_TEMPLATES = [ ] . freeze
68
-
69
- def initialize
70
- @data = SmallCache . new ( &KEY_BLOCK )
71
- end
72
-
73
- def inspect
74
- "#{ to_s [ 0 ..-2 ] } keys=#{ @data . size } >"
75
- end
76
-
77
- # Cache the templates returned by the block
78
- def cache ( key , name , prefix , partial , locals )
79
- @data [ key ] [ name ] [ prefix ] [ partial ] [ locals ] ||= canonical_no_templates ( yield )
80
- end
81
-
82
- def clear
83
- @data . clear
84
- end
85
-
86
- # Get the cache size. Do not call this
87
- # method. This method is not guaranteed to be here ever.
88
- def size # :nodoc:
89
- size = 0
90
- @data . each_value do |v1 |
91
- v1 . each_value do |v2 |
92
- v2 . each_value do |v3 |
93
- v3 . each_value do |v4 |
94
- size += v4 . size
95
- end
96
- end
97
- end
98
- end
99
-
100
- size
101
- end
102
-
103
- private
104
- def canonical_no_templates ( templates )
105
- templates . empty? ? NO_TEMPLATES : templates
106
- end
107
- end
108
-
109
52
cattr_accessor :caching , default : true
110
53
111
54
class << self
112
55
alias :caching? :caching
113
56
end
114
57
115
- def initialize
116
- @cache = Cache . new
117
- end
118
-
119
58
def clear_cache
120
- @cache . clear
121
59
end
122
60
123
61
# Normalizes the arguments and passes it on to find_templates.
124
62
def find_all ( name , prefix = nil , partial = false , details = { } , key = nil , locals = [ ] )
125
- locals = locals . map ( &:to_s ) . sort! . freeze
126
-
127
- cached ( key , [ name , prefix , partial ] , details , locals ) do
128
- _find_all ( name , prefix , partial , details , key , locals )
129
- end
63
+ _find_all ( name , prefix , partial , details , key , locals )
130
64
end
131
65
132
66
def all_template_paths # :nodoc:
@@ -147,22 +81,6 @@ def _find_all(name, prefix, partial, details, key, locals)
147
81
def find_templates ( name , prefix , partial , details , locals = [ ] )
148
82
raise NotImplementedError , "Subclasses must implement a find_templates(name, prefix, partial, details, locals = []) method"
149
83
end
150
-
151
- # Handles templates caching. If a key is given and caching is on
152
- # always check the cache before hitting the resolver. Otherwise,
153
- # it always hits the resolver but if the key is present, check if the
154
- # resolver is fresher before returning it.
155
- def cached ( key , path_info , details , locals )
156
- name , prefix , partial = path_info
157
-
158
- if key
159
- @cache . cache ( key , name , prefix , partial , locals ) do
160
- yield
161
- end
162
- else
163
- yield
164
- end
165
- end
166
84
end
167
85
168
86
# A resolver that loads files from the filesystem.
@@ -204,16 +122,12 @@ def all_template_paths # :nodoc:
204
122
205
123
private
206
124
def _find_all ( name , prefix , partial , details , key , locals )
207
- path = TemplatePath . build ( name , prefix , partial )
208
- requested_details = TemplateDetails ::Requested . new ( **details )
209
- query ( path , requested_details , locals , cache : !!key )
210
- end
211
-
212
- def query ( path , requested_details , locals , cache :)
213
- cache = cache ? @unbound_templates : Concurrent ::Map . new
125
+ requested_details = key || TemplateDetails ::Requested . new ( **details )
126
+ cache = key ? @unbound_templates : Concurrent ::Map . new
214
127
215
128
unbound_templates =
216
- cache . compute_if_absent ( path . virtual ) do
129
+ cache . compute_if_absent ( TemplatePath . virtual ( name , prefix , partial ) ) do
130
+ path = TemplatePath . build ( name , prefix , partial )
217
131
unbound_templates_from_path ( path )
218
132
end
219
133
0 commit comments