Parent

Files

Class Index [+]

Quicksearch

UbiquoDesign::CacheManagers::Base

Base class for widget cache

Public Class Methods

cache(widget_id, contents, options = {}) click to toggle source

Caches the content of a widget, with a possible expiration date.

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 78
78:         def cache(widget_id, contents, options = {})
79:           key = calculate_key(widget_id, options)
80:           validate(widget_id, key, options)
81:           if key
82:             Rails.logger.debug "Widget cache store request sent for widget: #{widget_id.to_s} with key #{key}"
83:             #check if expires_in is present and set it to store call
84:             store(key, contents, get_expiration_time(widget_id, options))
85:           else
86:             Rails.logger.debug "Widget cache missing policies for widget: #{widget_id.to_s}"
87:           end
88:         end
expire(widget_id, options = {}) click to toggle source

Expires the applicable content of a widget given its id

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 91
 91:         def expire(widget_id, options = {})
 92:           Rails.logger.debug "-- cache EXPIRATION --"
 93:           begin
 94:             model_key = calculate_key(widget_id, options.slice(:scope))
 95:             delete(model_key) if model_key
 96: 
 97:             with_instance_content(widget_id, options) do |instance_key|
 98:               keys = retrieve(instance_key)[:keys] rescue []
 99:               keys.each{|key| delete(key)}
100:               delete(instance_key)
101:             end
102:           rescue CacheNotAvailable
103:           end
104:         end
get(widget_id, options = {}) click to toggle source

Gets the cached content of a widget. Returns false if this widget is not currently cached

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 26
26:         def get(widget_id, options = {})
27:           if (key = calculate_key(widget_id, options))
28:             valid = not_expired(widget_id, key, options)
29:             if valid
30:               cached_content = retrieve(key)
31:               if cached_content
32:                 Rails.logger.debug "Widget cache hit for widget: #{widget_id.to_s} with key #{key}"
33:               else
34:                 Rails.logger.debug "Widget cache miss for widget: #{widget_id.to_s} with key #{key}"
35:               end
36:               cached_content
37:             end
38:           end
39:         end
multi_get(page, options = {}) click to toggle source

Gets all of the cached widgets content of a web. Return a hash where the key is the id of the widget and the value is the content

    # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 43
43:         def multi_get(page, options = {})
44:           widgets_with_key = {}
45:           all_widgets = []
46:           page.blocks.each do |block|
47:             block.real_block.widgets.each do |widget|
48:               key = calculate_key(widget, options)
49:               all_widgets << [widget, key] if key
50:             end
51:           end
52: 
53:           valid_widgets = validate_parents(all_widgets, options)
54:           valid_widgets.each do |elems|
55:             widgets_with_key[elems[0].id] = elems[1]
56:           end
57:           
58:           crypted_table = crypt_all_keys(widgets_with_key.values)
59:           
60:           cached_widgets = begin
61:             multi_retrieve crypted_table.keys
62:           rescue CacheNotAvailable
63:             return {}
64:           end
65: 
66:           widgets = {} 
67:           cached_widgets.each do |cached_widget|
68:             if cached_widget.last
69:               key = widgets_with_key.index(crypted_table[cached_widget.first])
70:               Rails.logger.debug "Widget cache hit for widget with id: #{key} with key #{crypted_table[cached_widget.first]}"
71:               widgets[key] = cached_widget.last
72:             end
73:           end
74:           widgets
75:         end

Protected Class Methods

calculate_key(widget, options = {}) click to toggle source

Calculates a string content identifier depending on the widget widget can be either a Widget instance or a widget id possible options:

  policy_context:  cache Policies definition context (default nil)
  scope:    object where the params and lambdas will be evaluated

Returns nil if the widget should not be cached according to the policies

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 114
114:         def calculate_key(widget, options = {})
115:           widget, policies = policies_for_widget(widget, options)
116:           return unless policies
117:           key = "#{widget.id.to_s}_#{widget.version || 0}"
118:           options[:widget] = widget
119:           policies[:models].each do |_key, val|
120:             if options[:scope].respond_to?(:params) || _key == options[:scope].class.name
121:               key += process_params(policies[:models][_key], options)
122:               key += process_procs(policies[:models][_key], options)
123:             end
124:           end
125:           if options[:scope].respond_to?(:params)
126:             key += process_params(policies, options)
127:             key += process_procs(policies, options)
128:           end
129:           key
130:         end
crypt_all_keys(values) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 337
337:         def crypt_all_keys(values)
338:           ret_set = {}
339:           values.each do |val|
340:             ret_set[crypted_key(val)] = val
341:           end
342:           ret_set
343:         end
crypt_all_parents_keys(widget_hash) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 345
345:         def crypt_all_parents_keys(widget_hash)
346:           ret_set = {}
347:           widget_hash.each do |key, val|
348:             vals = []
349:             val.each do |non_crypted|
350:               c_key =  crypted_key(non_crypted)
351:               vals << c_key
352:               ret_set[c_key] = non_crypted 
353:             end
354:             widget_hash[key] = vals
355:           end
356:           ret_set
357:         end
crypted_key(key) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 332
332:         def crypted_key key
333:           obj = Digest::SHA256.new << key
334:           obj.to_s
335:         end
delete(key) click to toggle source

removes the widget content from the store

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 203
203:         def delete(key)
204:           raise NotImplementedError.new 'Implement delete(key) in your CacheManager'
205:         end
get_expiration_time(widget, options) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 326
326:         def get_expiration_time widget, options
327:           widget = widget.is_a?(Widget) ? widget : Widget.find(widget)
328:           policies = UbiquoDesign::CachePolicies.get(options[:policy_context])[widget.key]
329:           policies[:expires_in]
330:         end
get_parents(all_widgets, options) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 265
265:         def get_parents all_widgets, options
266:           parents = {} #each parent
267:           all_widgets.each do |widget|
268:             options[:widget] = widget
269:             parents[widget.id] = []
270:             with_instance_content(widget, options) do |ikey|
271:               parents[widget.id] << ikey
272:             end
273:           end
274:           return parents
275:         end
not_expired(widget, key, options) click to toggle source

Returns true if the key fragment is not expired and still vigent

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 208
208:         def not_expired(widget, key, options)
209:           with_instance_content(widget, options) do |instance_key|
210:             valid_keys = retrieve(instance_key)
211:             begin
212:               if valid_keys.blank? || !valid_keys[:keys].include?(key)
213:                 return false
214:               end
215:             rescue
216:               return false
217:             end
218:           end
219:           true
220:         end
own_parents_valid(parents, current_keys, own_key) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 257
257:         def own_parents_valid parents, current_keys, own_key
258:           current_keys.each do |ck|
259:             val = parents[ck]
260:             return false if val.blank? || !val[:keys].include?(own_key)
261:           end
262:           return true
263:         end
policies_for_widget(widget, options) click to toggle source

Returns a widget and its policies ([widget, policies]) for a given widget or widget_id

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 320
320:         def policies_for_widget widget, options
321:           widget = widget.is_a?(Widget) ? widget : Widget.find(widget)
322:           policies = UbiquoDesign::CachePolicies.get(options[:policy_context])[widget.key]
323:           [widget, policies]
324:         end
process_params(policies, options) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 132
132:         def process_params policies, options
133:           params_key = ''
134:           if policies[:params].present? || policies[:widget_params].present? 
135:             param_ids = policies[:params].map do |param_id_raw|
136:               param_id, t_param_id = case param_id_raw
137:               when Symbol
138:                 [param_id_raw, param_id_raw]
139:               when Hash
140:                 if options[:scope].respond_to?(:params)
141:                   [param_id_raw.keys.first(),
142:                    param_id_raw.keys.first()]
143:                 else
144:                   [param_id_raw.keys.first(),
145:                    param_id_raw.values.first()]
146:                 end
147:               end
148:               if options[:scope].respond_to?(:params)
149:                 "###{param_id}###{options[:scope].send(:params)[t_param_id]}"
150:               else
151:                 "###{param_id}###{options[:scope].send(t_param_id)}"
152:               end
153:             end
154:             if policies[:widget_params].present?
155:               param_ids << "c_params_" + options[:scope].params.map{|key, val| "#{key}@#{val}" }.sort.join("&")
156:             end
157:             params_key = '_params_' + param_ids.join
158:           end
159:           params_key
160:         end
process_procs(policies, options) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 162
162:         def process_procs policies, options
163:           procs_key = ''
164:           if policies[:procs].present?
165:             proc_ids = policies[:procs].map do |proc_raw|
166:               proc = case proc_raw
167:               when Proc
168:                 next unless options[:scope].respond_to?(:params)
169:                 proc_raw
170:               when Array
171:                 if options[:scope].respond_to?(:params)
172:                   proc_raw.first
173:                 else
174:                   proc_raw.last
175:                 end
176:               end
177:               if proc.is_a?(Proc)
178:                 "###{proc.bind(options[:scope]).call(options[:widget])}"
179:               elsif proc.is_a?(Symbol)
180:                 if options[:scope].respond_to?(:params)
181:                   "###{options[:scope].send(:params)[proc]}"
182:                 else
183:                   "###{options[:scope].send(proc)}"
184:                 end
185:               end
186:             end
187:             procs_key = '_procs_' + proc_ids.join if proc_ids.compact.present?
188:           end
189:           procs_key
190:         end
retrieve(key) click to toggle source

retrieves the widget content identified by key

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 193
193:         def retrieve(key)
194:           raise NotImplementedError.new 'Implement retrieve(key) in your CacheManager'
195:         end
store(key, contents) click to toggle source

Stores a widget content indexing by a key

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 198
198:         def store(key, contents)
199:           raise NotImplementedError.new 'Implement store(key, contents) in your CacheManager'
200:         end
validate(widget, key, options) click to toggle source

Marks the key as valid if necessary

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 223
223:         def validate(widget, key, options)
224:           with_instance_content(widget, options) do |instance_key|
225:             valid_keys = begin
226:               retrieve(instance_key)
227:             rescue CacheNotAvailable
228:               return {}
229:             end
230:             valid_keys ||= {}
231:             (valid_keys[:keys] ||= []) << key
232:             valid_keys[:keys].uniq
233:             store(instance_key, valid_keys, get_expiration_time(widget, options))
234:           end
235: 
236:         end
validate_parents(all_widgets, options) click to toggle source

(Not documented)

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 238
238:         def validate_parents(all_widgets, options)
239:           valid_widgets = []
240:           parents = get_parents(all_widgets.map{|lk| lk[0]}, options)
241: 
242:           crypted_table = crypt_all_parents_keys(parents) 
243:           begin 
244:             cached_parents = multi_retrieve crypted_table.keys
245:             crypted_keys = crypted_table.keys
246:             all_widgets.each_with_index do |widget, index|
247:               current_keys = parents[widget[0].id]
248:               valid_widgets << widget if own_parents_valid(cached_parents, current_keys, widget[1])
249:             end
250:           rescue CacheNotAvailable
251:             valid_widget = []
252:           end
253:           
254:           valid_widgets
255:         end
with_instance_content(widget, options) {|widget_pre_key + p_i| ...} click to toggle source

Wrapper for getting, if applicable, the instance content id, given a Widget instance

     # File vendor/plugins/ubiquo_design/lib/ubiquo_design/cache_managers/base.rb, line 279
279:         def with_instance_content(widget, options)
280:           widget, policies = policies_for_widget(widget, options)
281:           return unless policies
282: 
283:           widget_pre_key = "__" + (widget.is_a?(Widget) ? widget.id.to_s : widget.to_s )
284:           if policies[:models].present?
285:             policies[:models].each do |key, val|
286:               if options[:current_model].present? && options[:current_model].to_s != key.to_s
287:                 next
288:               end
289:               if val[:identifier].is_a?(Hash)
290:                 if options[:scope].respond_to?(:params)
291:                   true_identifier = val[:identifier].keys.first
292:                 else
293:                   true_identifier = val[:identifier].values.first
294:                 end
295:               elsif val[:identifier].is_a?(Array)
296:                 if options[:scope].respond_to?(:params)
297:                   true_identifier = val[:identifier].first
298:                 else
299:                   true_identifier = val[:identifier].last
300:                 end
301:               else
302:                 true_identifier = val[:identifier]
303:               end
304: 
305:               p_i = key.to_s + '_' 
306:               p_i += process_params(val, options)
307:               p_i += process_procs(val, options)
308:               yield(widget_pre_key + p_i)
309:             end
310:           else
311:             yield(widget_pre_key)
312:           end
313:           return
314: 
315:         end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.