Module: Hancock::Cache::CacheHelper

Defined in:
app/helpers/hancock/cache/cache_helper.rb

Instance Method Summary collapse

Instance Method Details

#hancock_cache(obj = @virtual_path, options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/hancock/cache/cache_helper.rb', line 25

def hancock_cache(obj = @virtual_path, options = {}, &block)
  if obj.is_a?(String) or obj.is_a?(Symbol)
    return hancock_fragment_cache obj.to_s, (options || {}).merge(skip_digest: true), &block

  else
    condition = Array(obj).map { |o|
      !o.respond_to?(:perform_caching) or o.perform_caching
    }

    if obj.respond_to?(:cache_keys_str) and obj.respond_to?(:cache_keys) and obj.respond_to?(:cache_keys=)
      obj.cache_keys |= hancock_cache_views_keys
      obj.save if obj.cache_keys_str_changed?
    end

    condition = (condition.blank? or condition.uniq == [true])
    cache_if condition, obj, options, &block
  end
end

#hancock_cache_if(condition, obj = [], options = {}, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/helpers/hancock/cache/cache_helper.rb', line 46

def hancock_cache_if(condition, obj = [], options = {}, &block)
  if condition
    hancock_cache obj, options, &block
  else
    yield
  end
  nil
end

#hancock_cache_keysObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/hancock/cache/cache_helper.rb', line 141

def hancock_cache_keys
  ret = lookup_context.hancock_cache_keys.dup

  if respond_to?(:page_cache_key) and !page_cache_key.blank?
    if (!respond_to?(:page_cache_obj) or page_cache_obj.nil?)
      _name = page_cache_key
      _desc = "        action caching\n        controller: \#{controller_name}\n        action: \#{action_name}\n        params: \#{params.inspect}\n      TEXT\n      Hancock::Cache::Fragment.create_unless_exists(name: Hancock::Cache::Fragment.name_from_view(_name), desc: _desc)\n    end\n    ret.unshift page_cache_key\n  end\n\n  ret.uniq.freeze\nend\n"

#hancock_cache_settings(key, options = {}, &block) ⇒ Object Also known as: hancock_cache_setting



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/hancock/cache/cache_helper.rb', line 5

def hancock_cache_settings(key, options = {}, &block)
  if key.is_a?(Hash)
    key, options = key[:key], key
  end

  cache_keys = options[:cache_keys_str] || options[:cache_keys] || options[:cache_key] || []
  if cache_keys.is_a?(::Array)
    cache_keys = cache_keys.map { |k| k.to_s.strip }.join(" ")
  else
    cache_keys = cache_keys.to_s.strip
  end
  options.delete(:cache_keys)
  options.delete(:cache_key)
  options[:cache_keys_str] = [cache_keys, hancock_cache_views_keys].flatten.map { |k| k.to_s.strip }.reject(&:blank?).join(" ").strip

  hancock_settings(key, options, &block)
end

#hancock_cache_unless(condition, obj = [], options = {}, &block) ⇒ Object



43
44
45
# File 'app/helpers/hancock/cache/cache_helper.rb', line 43

def hancock_cache_unless(condition, obj = [], options = {}, &block)
  hancock_cache_if !condition, obj, options, &block
end

#hancock_cache_views_keysObject



161
162
163
# File 'app/helpers/hancock/cache/cache_helper.rb', line 161

def hancock_cache_views_keys
  hancock_cache_keys.map { |k| Hancock::Cache::Fragment.name_from_view(k) }
end

#hancock_fragment_cache(name = @virtual_path, options = {skip_digest: true}, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/hancock/cache/cache_helper.rb', line 55

def hancock_fragment_cache(name = @virtual_path, options = {skip_digest: true}, &block)
  if name.is_a?(Hash)
    name, options = name.delete(:name), name
  end
  name = @virtual_path if name.blank?
  for_object  = (options and options.delete(:for_object))
  for_objects = (options and options.delete(:for_objects))
  for_model   = (options and options.delete(:for_model))
  for_setting = (options and options.delete(:for_setting))
  _on_ram     = (options and options.delete(:on_ram))

  # if Hancock::Cache.config.model_settings_support
  #   _detect_cache = !!(Hancock::Cache.config.runtime_cache_detector || Hancock::Cache::Fragment.settings.detecting)
  # else
  #   _detect_cache = !!(Hancock::Cache.config.runtime_cache_detector || Settings.hancock_cache_detecting)
  # end
  _detect_cache = true

  if respond_to?(:hancock_cache_fragments)
    frag = hancock_cache_fragments[Hancock::Cache::Fragment.name_from_view(name)]
    # parents = hancock_cache_keys.map do |_name|
    #   hancock_cache_fragments[Hancock::Cache::Fragment.name_from_view(_name)]
    # end.compact
    parent_names = hancock_cache_views_keys
    if frag
      frag.set_for_object(for_object) if for_object
      frag.set_for_objects(for_objects) if for_objects
      frag.set_for_model(for_model) if for_model
      frag.set_for_setting(for_setting) if for_setting
      if _detect_cache
        frag.parent_names |= parent_names
        if frag.parent_names_changed?
          # frag.set_parent_ids! and hancock_cache_fragments[frag.name] = frag
          frag.update_parent_ids! and hancock_cache_fragments[frag.name] = frag.reload
        end
      end
    else
      if _detect_cache
        _name = Hancock::Cache::Fragment.name_from_view(name)
        _desc = "" #"#{@virtual_path}\noptions: #{options}"
        _virtual_path = @virtual_path
        # Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parents: parents)
        Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parent_names: parent_names, on_ram: _on_ram)
        Hancock::Cache::Fragment.reload!
        frag = hancock_cache_fragments[_name]
      end
    end
    condition = (frag and frag.enabled)
  else
    if _detect_cache
      # parents = Hancock::Cache::Fragment.by_name_from_view(hancock_cache_keys).to_a
      # _name = Hancock::Cache::Fragment.name_from_view(name)
      # _desc = "" #"#{@virtual_path}\noptions: #{options}"
      # _virtual_path = @virtual_path
      # Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parents: parents)
      # Hancock::Cache::Fragment.reload!
      # frag.parent_names |= parent_names
      # if frag.parent_names_changed?
      #   # frag.set_parent_ids! and hancock_cache_fragments[frag.name] = frag
      #   frag.update_parent_ids! and hancock_cache_fragments[frag.name] = frag
      # end
    end
    condition = Hancock::Cache::Fragment.enabled.by_name_from_view(name).count > 0
  end
  if frag and frag.on_ram and !frag.on_ram_data.nil?
    ret = frag.on_ram_data
  else
    lookup_context.hancock_cache_keys << name
    ret = cache_if condition, name, options, &block
    frag.on_ram_data = ret if frag and frag.on_ram
    lookup_context.hancock_cache_keys.delete(name)
  end
  return ret
end

#hancock_fragment_cache_if(condition, obj = [], options = {}, &block) ⇒ Object



132
133
134
135
136
137
138
139
# File 'app/helpers/hancock/cache/cache_helper.rb', line 132

def hancock_fragment_cache_if(condition, obj = [], options = {}, &block)
  if condition
    hancock_fragment_cache obj, options, &block
  else
    yield
  end
  nil
end

#hancock_fragment_cache_unless(condition, obj = [], options = {}, &block) ⇒ Object



129
130
131
# File 'app/helpers/hancock/cache/cache_helper.rb', line 129

def hancock_fragment_cache_unless(condition, obj = [], options = {}, &block)
  hancock_fragment_cache_if !condition, obj, options, &block
end