hancock_cms_cache 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/hancock/rails_admin/plugins/cache.coffee +4 -0
- data/app/assets/javascripts/hancock/rails_admin/plugins/cache/custom/ui.coffee +0 -0
- data/app/assets/stylesheets/hancock/rails_admin/plugins/cache.sass +3 -0
- data/app/assets/stylesheets/hancock/rails_admin/plugins/cache/custom/theming.sass +0 -0
- data/app/controllers/concerns/hancock/cache/action.rb +58 -0
- data/app/controllers/concerns/hancock/cache/ajax_csrf.rb +25 -0
- data/app/controllers/concerns/hancock/cache/fragments.rb +34 -0
- data/app/controllers/concerns/hancock/cache/no_cache.rb +12 -0
- data/app/helpers/hancock/cache/cache_helper.rb +167 -0
- data/app/models/concerns/hancock/cache/cacheable.rb +164 -0
- data/app/models/concerns/hancock/cache/cleared_stack.rb +43 -0
- data/app/models/concerns/hancock/cache/decorators/fragment.rb +41 -0
- data/app/models/concerns/hancock/cache/loadable.rb +44 -0
- data/app/models/concerns/hancock/cache/snapshotable.rb +49 -0
- data/app/models/hancock/cache/fragment.rb +11 -0
- data/app/views/hancock/_assets.html.slim +24 -0
- data/app/views/layouts/application.html.slim +33 -0
- data/app/views/rails_admin/main/hancock_cache_global_clear.html.slim +25 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/initializers/cache_detector.rb +53 -0
- data/config/initializers/hancock_cache.rb +7 -0
- data/config/locales/hancock.cache.ru.yml +59 -0
- data/hancock_cms_cache.gemspec +34 -0
- data/lib/generators/hancock/cache/config/config_generator.rb +13 -0
- data/lib/generators/hancock/cache/config/templates/hancock_cache.erb +14 -0
- data/lib/generators/hancock/cache/models/decorators_generator.rb +24 -0
- data/lib/generators/hancock/cache/models/fragment_generator.rb +39 -0
- data/lib/generators/hancock/cache/models/templates/fragment.erb +38 -0
- data/lib/hancock/cache/admin.rb +37 -0
- data/lib/hancock/cache/admin/fragment.rb +140 -0
- data/lib/hancock/cache/configuration.rb +38 -0
- data/lib/hancock/cache/engine.rb +20 -0
- data/lib/hancock/cache/models/fragment.rb +310 -0
- data/lib/hancock/cache/models/mongoid/fragment.rb +259 -0
- data/lib/hancock/cache/rails_admin_ext/hancock_cache_clear.rb +95 -0
- data/lib/hancock/cache/rails_admin_ext/hancock_cache_dump_snapshot.rb +92 -0
- data/lib/hancock/cache/rails_admin_ext/hancock_cache_global_clear.rb +74 -0
- data/lib/hancock/cache/rails_admin_ext/hancock_cache_restore_snapshot.rb +92 -0
- data/lib/hancock/cache/rails_admin_ext/hancock_touch.rb +95 -0
- data/lib/hancock/cache/rails_admin_settings_patch.rb +185 -0
- data/lib/hancock/cache/version.rb +5 -0
- data/lib/hancock_cms_cache.rb +34 -0
- data/release.sh +6 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8924cc51d91eba2c74ffdbc5653ece7445b465dd
|
4
|
+
data.tar.gz: 64e32fb9925538ad64d541b54130e3932ada290c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0802fed32cfd539c32ee4d046e11b8261ebc821524a2f620c9e3a519878c8471c54a12858013e518cdc00fc13cb8b4fef9b309836c2b121e16cc61eec333679b'
|
7
|
+
data.tar.gz: 4db3c7530a071c6f63cb5ac17d0fb07cfb821837fef54af39805a433756429cb29aa93120886ee075b874ebc9f911e544afd16c0083230d0e3f28684364de876
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
hancock_cms_cache
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.3
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# HancockCmsCache
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hancock/cache`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hancock_cms_cache'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hancock_cms_cache
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/red-rocks/hancock_cms_cache.
|
data/Rakefile
ADDED
File without changes
|
File without changes
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Hancock::Cache::Action
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
def page_cache_key
|
6
|
+
"pages/#{controller_name}_#{action_name}" if (params.keys - ["controller", "action", "rack.session"]).blank?
|
7
|
+
end
|
8
|
+
def page_cache_obj
|
9
|
+
return @page_cache_obj unless @page_cache_obj.nil?
|
10
|
+
_name = page_cache_key
|
11
|
+
_desc = <<-TEXT
|
12
|
+
action caching
|
13
|
+
controller: #{controller_name}
|
14
|
+
action: #{action_name}
|
15
|
+
params: #{params.inspect}
|
16
|
+
TEXT
|
17
|
+
@page_cache_obj = Hancock::Cache::Fragment.create_unless_exists(name: Hancock::Cache::Fragment.name_from_view(_name), desc: _desc, is_action_cache: true)
|
18
|
+
end
|
19
|
+
|
20
|
+
def hancock_stale?(obj, opts = {})
|
21
|
+
return if obj.nil?
|
22
|
+
if obj.is_a?(Hash)
|
23
|
+
return stale?(obj)
|
24
|
+
end
|
25
|
+
if obj
|
26
|
+
_last_time = opts[:last_modified]
|
27
|
+
if _last_time.nil?
|
28
|
+
_methods = ([:u_at, :updated_at, :c_at, :created_at] + (opts[:last_modified_methods] || [])).uniq
|
29
|
+
_methods.each do |m|
|
30
|
+
_last_time ||= obj.send(m) if obj.respond_to?(m)
|
31
|
+
break if _last_time
|
32
|
+
end
|
33
|
+
_last_time ||= Time.new
|
34
|
+
end
|
35
|
+
opts.reverse_merge!(last_modified: _last_time, etag: obj, public: true, template: false)
|
36
|
+
return stale?(obj)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
alias_method :hancock_cache_stale?, :hancock_stale?
|
40
|
+
|
41
|
+
def hancock_cache_fragment_stale?(name, opts = {})
|
42
|
+
if name.is_a?(Hash)
|
43
|
+
name, opts = name.delete(:name), name
|
44
|
+
end
|
45
|
+
return if name.nil?
|
46
|
+
frag = hancock_cache_fragments[Hancock::Cache::Fragment.name_from_view(name)]
|
47
|
+
if frag
|
48
|
+
opts.reverse_merge!(last_modified: frag.last_clear_time, etag: frag, public: true, template: false)
|
49
|
+
hancock_stale?(frag, opts.compact)
|
50
|
+
else
|
51
|
+
true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
helper_method :page_cache_key, :page_cache_obj
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Hancock::Cache::AjaxCsrf
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include Hancock::Cache::NoCache
|
6
|
+
skip_before_action :find_page, only: [:csrf_meta]
|
7
|
+
|
8
|
+
def csrf_meta
|
9
|
+
|
10
|
+
set_cache_buster
|
11
|
+
# expires_now
|
12
|
+
# response.headers["Pragma"] = "no-cache"
|
13
|
+
|
14
|
+
respond_to do |format|
|
15
|
+
format.json do
|
16
|
+
render json: {
|
17
|
+
param: request_forgery_protection_token,
|
18
|
+
token: form_authenticity_token
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Hancock::Cache::Fragments
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included do
|
4
|
+
@@hancock_cache_fragments = nil
|
5
|
+
# before_action :load_fragments
|
6
|
+
|
7
|
+
helper_method :hancock_cache_fragments
|
8
|
+
def hancock_cache_fragments(reload = false)
|
9
|
+
# @hancock_cache_fragments ||= Hancock::Cache::Fragment.cutted.all.to_a.map { |f| [f.name, f] }.to_h
|
10
|
+
if reload
|
11
|
+
Hancock::Cache::Fragment.reload!
|
12
|
+
@@hancock_cache_fragments = Hancock::Cache::Fragment.loaded_info
|
13
|
+
elsif Hancock::Cache::Fragment.loaded
|
14
|
+
@@hancock_cache_fragments ||= Hancock::Cache::Fragment.loaded_info
|
15
|
+
else
|
16
|
+
Hancock::Cache::Fragment.load!
|
17
|
+
@@hancock_cache_fragments = Hancock::Cache::Fragment.loaded_info
|
18
|
+
end
|
19
|
+
@@hancock_cache_fragments
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class_methods do
|
24
|
+
def reload_fragments
|
25
|
+
Hancock::Cache::Fragment.reload!
|
26
|
+
@@hancock_cache_fragments = Hancock::Cache::Fragment.loaded_info
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
def load_fragments
|
32
|
+
hancock_cache_fragments(true)
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
module Hancock
|
2
|
+
module Cache
|
3
|
+
module CacheHelper
|
4
|
+
|
5
|
+
def hancock_cache_settings(key, options = {}, &block)
|
6
|
+
if key.is_a?(Hash)
|
7
|
+
key, options = key[:key], key
|
8
|
+
end
|
9
|
+
|
10
|
+
cache_keys = options[:cache_keys_str] || options[:cache_keys] || options[:cache_key] || []
|
11
|
+
if cache_keys.is_a?(::Array)
|
12
|
+
cache_keys = cache_keys.map { |k| k.to_s.strip }.join(" ")
|
13
|
+
else
|
14
|
+
cache_keys = cache_keys.to_s.strip
|
15
|
+
end
|
16
|
+
options.delete(:cache_keys)
|
17
|
+
options.delete(:cache_key)
|
18
|
+
options[:cache_keys_str] = [cache_keys, hancock_cache_views_keys].flatten.map { |k| k.to_s.strip }.reject(&:blank?).join(" ").strip
|
19
|
+
|
20
|
+
hancock_settings(key, options, &block)
|
21
|
+
end
|
22
|
+
alias_method :hancock_cache_setting, :hancock_cache_settings
|
23
|
+
|
24
|
+
|
25
|
+
def hancock_cache(obj = @virtual_path, options = {}, &block)
|
26
|
+
if obj.is_a?(String) or obj.is_a?(Symbol)
|
27
|
+
return hancock_fragment_cache obj.to_s, (options || {}).merge(skip_digest: true), &block
|
28
|
+
|
29
|
+
else
|
30
|
+
condition = Array(obj).map { |o|
|
31
|
+
!o.respond_to?(:perform_caching) or o.perform_caching
|
32
|
+
}
|
33
|
+
|
34
|
+
if obj.respond_to?(:cache_keys_str) and obj.respond_to?(:cache_keys) and obj.respond_to?(:cache_keys=)
|
35
|
+
obj.cache_keys |= hancock_cache_views_keys
|
36
|
+
obj.save if obj.cache_keys_str_changed?
|
37
|
+
end
|
38
|
+
|
39
|
+
condition = (condition.blank? or condition.uniq == [true])
|
40
|
+
cache_if condition, obj, options, &block
|
41
|
+
end
|
42
|
+
end
|
43
|
+
def hancock_cache_unless(condition, obj = [], options = {}, &block)
|
44
|
+
hancock_cache_if !condition, obj, options, &block
|
45
|
+
end
|
46
|
+
def hancock_cache_if(condition, obj = [], options = {}, &block)
|
47
|
+
if condition
|
48
|
+
hancock_cache obj, options, &block
|
49
|
+
else
|
50
|
+
yield
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def hancock_fragment_cache(name = @virtual_path, options = {skip_digest: true}, &block)
|
56
|
+
if name.is_a?(Hash)
|
57
|
+
name, options = name.delete(:name), name
|
58
|
+
end
|
59
|
+
name = @virtual_path if name.blank?
|
60
|
+
for_object = (options and options.delete(:for_object))
|
61
|
+
for_objects = (options and options.delete(:for_objects))
|
62
|
+
for_model = (options and options.delete(:for_model))
|
63
|
+
for_setting = (options and options.delete(:for_setting))
|
64
|
+
_on_ram = (options and options.delete(:on_ram))
|
65
|
+
|
66
|
+
# if Hancock::Cache.config.model_settings_support
|
67
|
+
# _detect_cache = !!(Hancock::Cache.config.runtime_cache_detector || Hancock::Cache::Fragment.settings.detecting)
|
68
|
+
# else
|
69
|
+
# _detect_cache = !!(Hancock::Cache.config.runtime_cache_detector || Settings.hancock_cache_detecting)
|
70
|
+
# end
|
71
|
+
_detect_cache = true
|
72
|
+
|
73
|
+
if respond_to?(:hancock_cache_fragments)
|
74
|
+
frag = hancock_cache_fragments[Hancock::Cache::Fragment.name_from_view(name)]
|
75
|
+
# parents = hancock_cache_keys.map do |_name|
|
76
|
+
# hancock_cache_fragments[Hancock::Cache::Fragment.name_from_view(_name)]
|
77
|
+
# end.compact
|
78
|
+
parent_names = hancock_cache_views_keys
|
79
|
+
if frag
|
80
|
+
frag.set_for_object(for_object) if for_object
|
81
|
+
frag.set_for_objects(for_objects) if for_objects
|
82
|
+
frag.set_for_model(for_model) if for_model
|
83
|
+
frag.set_for_setting(for_setting) if for_setting
|
84
|
+
if _detect_cache
|
85
|
+
frag.parent_names |= parent_names
|
86
|
+
if frag.parent_names_changed?
|
87
|
+
# frag.set_parent_ids! and hancock_cache_fragments[frag.name] = frag
|
88
|
+
frag.update_parent_ids! and hancock_cache_fragments[frag.name] = frag.reload
|
89
|
+
end
|
90
|
+
end
|
91
|
+
else
|
92
|
+
if _detect_cache
|
93
|
+
_name = Hancock::Cache::Fragment.name_from_view(name)
|
94
|
+
_desc = "" #"#{@virtual_path}\noptions: #{options}"
|
95
|
+
_virtual_path = @virtual_path
|
96
|
+
# Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parents: parents)
|
97
|
+
Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parent_names: parent_names, on_ram: _on_ram)
|
98
|
+
Hancock::Cache::Fragment.reload!
|
99
|
+
frag = hancock_cache_fragments[_name]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
condition = (frag and frag.enabled)
|
103
|
+
else
|
104
|
+
if _detect_cache
|
105
|
+
# parents = Hancock::Cache::Fragment.by_name_from_view(hancock_cache_keys).to_a
|
106
|
+
# _name = Hancock::Cache::Fragment.name_from_view(name)
|
107
|
+
# _desc = "" #"#{@virtual_path}\noptions: #{options}"
|
108
|
+
# _virtual_path = @virtual_path
|
109
|
+
# Hancock::Cache::Fragment.create_unless_exists(name: _name, desc: _desc, virtual_path: _virtual_path, parents: parents)
|
110
|
+
# Hancock::Cache::Fragment.reload!
|
111
|
+
# frag.parent_names |= parent_names
|
112
|
+
# if frag.parent_names_changed?
|
113
|
+
# # frag.set_parent_ids! and hancock_cache_fragments[frag.name] = frag
|
114
|
+
# frag.update_parent_ids! and hancock_cache_fragments[frag.name] = frag
|
115
|
+
# end
|
116
|
+
end
|
117
|
+
condition = Hancock::Cache::Fragment.enabled.by_name_from_view(name).count > 0
|
118
|
+
end
|
119
|
+
if frag and frag.on_ram and !frag.on_ram_data.nil?
|
120
|
+
ret = frag.on_ram_data
|
121
|
+
else
|
122
|
+
lookup_context.hancock_cache_keys << name
|
123
|
+
ret = cache_if condition, name, options, &block
|
124
|
+
frag.on_ram_data = ret if frag and frag.on_ram
|
125
|
+
lookup_context.hancock_cache_keys.delete(name)
|
126
|
+
end
|
127
|
+
return ret
|
128
|
+
end
|
129
|
+
def hancock_fragment_cache_unless(condition, obj = [], options = {}, &block)
|
130
|
+
hancock_fragment_cache_if !condition, obj, options, &block
|
131
|
+
end
|
132
|
+
def hancock_fragment_cache_if(condition, obj = [], options = {}, &block)
|
133
|
+
if condition
|
134
|
+
hancock_fragment_cache obj, options, &block
|
135
|
+
else
|
136
|
+
yield
|
137
|
+
end
|
138
|
+
nil
|
139
|
+
end
|
140
|
+
|
141
|
+
def hancock_cache_keys
|
142
|
+
ret = lookup_context.hancock_cache_keys.dup
|
143
|
+
|
144
|
+
if respond_to?(:page_cache_key) and !page_cache_key.blank?
|
145
|
+
if (!respond_to?(:page_cache_obj) or page_cache_obj.nil?)
|
146
|
+
_name = page_cache_key
|
147
|
+
_desc = <<-TEXT
|
148
|
+
action caching
|
149
|
+
controller: #{controller_name}
|
150
|
+
action: #{action_name}
|
151
|
+
params: #{params.inspect}
|
152
|
+
TEXT
|
153
|
+
Hancock::Cache::Fragment.create_unless_exists(name: Hancock::Cache::Fragment.name_from_view(_name), desc: _desc)
|
154
|
+
end
|
155
|
+
ret.unshift page_cache_key
|
156
|
+
end
|
157
|
+
|
158
|
+
ret.uniq.freeze
|
159
|
+
end
|
160
|
+
|
161
|
+
def hancock_cache_views_keys
|
162
|
+
hancock_cache_keys.map { |k| Hancock::Cache::Fragment.name_from_view(k) }
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
if Hancock.mongoid?
|
2
|
+
|
3
|
+
module Hancock::Cache::Cacheable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
field :cache_keys_str, type: String, default: -> { default_cache_keys.map(&:strip).join("\n") }
|
8
|
+
def self.default_cache_keys
|
9
|
+
[]
|
10
|
+
end
|
11
|
+
def default_cache_keys
|
12
|
+
self.class.default_cache_keys
|
13
|
+
end
|
14
|
+
def set_default_cache_keys(strategy = :append)
|
15
|
+
_old_keys = cache_keys
|
16
|
+
|
17
|
+
_keys = case strategy.to_sym
|
18
|
+
when :append
|
19
|
+
(_old_keys + default_cache_keys).uniq
|
20
|
+
when :overwrite, :replace
|
21
|
+
default_cache_keys
|
22
|
+
else
|
23
|
+
_old_keys
|
24
|
+
end
|
25
|
+
self
|
26
|
+
end
|
27
|
+
def set_default_cache_keys!(strategy = :append)
|
28
|
+
self.set_default_cache_keys(strategy) and self.save
|
29
|
+
end
|
30
|
+
def self.set_default_cache_keys(strategy = :append)
|
31
|
+
self.all.to_a.map { |obj| obj.set_default_cache_keys(strategy) }
|
32
|
+
end
|
33
|
+
def self.set_default_cache_keys!(strategy = :append)
|
34
|
+
self.all.to_a.map { |obj| obj.set_default_cache_keys!(strategy) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def conditional_cache_keys
|
38
|
+
[{}]
|
39
|
+
end
|
40
|
+
def conditional_cache_keys_names
|
41
|
+
conditional_cache_keys.map { |k| k[:name] }.compact
|
42
|
+
end
|
43
|
+
def selected_conditional_cache_keys
|
44
|
+
conditional_cache_keys and conditional_cache_keys.select { |cache_key|
|
45
|
+
false if cache_key[:name].blank?
|
46
|
+
cond_if, cond_unless = cache_key[:if], cache_key[:unless]
|
47
|
+
|
48
|
+
if cond_if
|
49
|
+
if cond_if.is_a?(Proc)
|
50
|
+
cond_if = !!cond_if.call
|
51
|
+
elsif cond_if.is_a?(String)
|
52
|
+
cond_if = !!self.instance_eval(cond_if)
|
53
|
+
else
|
54
|
+
cond_if = !!cond_if
|
55
|
+
end
|
56
|
+
else
|
57
|
+
cond_if = true
|
58
|
+
end
|
59
|
+
|
60
|
+
if cond_unless
|
61
|
+
if cond_unless.is_a?(Proc)
|
62
|
+
cond_unless = !cond_unless.call
|
63
|
+
elsif cond_unless.is_a?(String)
|
64
|
+
cond_unless = !self.instance_eval(cond_unless)
|
65
|
+
else
|
66
|
+
cond_unless = !cond_unless
|
67
|
+
end
|
68
|
+
else
|
69
|
+
cond_unless = true
|
70
|
+
end
|
71
|
+
|
72
|
+
cond_if and cond_unless
|
73
|
+
} or []
|
74
|
+
end
|
75
|
+
def selected_conditional_cache_keys_names
|
76
|
+
selected_conditional_cache_keys.map { |k| k[:name] }.compact
|
77
|
+
end
|
78
|
+
|
79
|
+
class_attribute :forced_cache_keys
|
80
|
+
self.forced_cache_keys ||= []
|
81
|
+
|
82
|
+
def self.add_forced_cache_key(key)
|
83
|
+
key = key.to_s
|
84
|
+
self.forced_cache_keys << key unless self.forced_cache_keys.include?(key)
|
85
|
+
end
|
86
|
+
def self.remove_forced_cache_key(key)
|
87
|
+
self.forced_cache_keys.delete(key.to_s)
|
88
|
+
end
|
89
|
+
|
90
|
+
def all_cache_keys(cached = true)
|
91
|
+
# return @all_cache_keys if @all_cache_keys
|
92
|
+
if cached
|
93
|
+
@all_cache_keys = cache_keys || []
|
94
|
+
@all_cache_keys += (forced_cache_keys || [])
|
95
|
+
@all_cache_keys += selected_conditional_cache_keys_names
|
96
|
+
@all_cache_keys
|
97
|
+
else
|
98
|
+
(cache_keys || []) + selected_conditional_cache_keys_names
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
def cache_keys
|
104
|
+
# return @cache_keys if @cache_keys
|
105
|
+
|
106
|
+
self.cache_keys_str ||= ""
|
107
|
+
# @cache_keys = cache_keys_str.split(/\s+/).map { |k| k.strip }.reject { |k| k.blank? }
|
108
|
+
self.cache_keys_str.split(/\s+/).map { |k| k.strip }.reject { |k| k.blank? }
|
109
|
+
end
|
110
|
+
def cache_keys=(_keys)
|
111
|
+
_keys ||= []
|
112
|
+
self.cache_keys_str = _keys.select { |k|
|
113
|
+
k and !k.strip.blank?
|
114
|
+
}.map(&:strip).uniq.join("\n")
|
115
|
+
cache_keys
|
116
|
+
end
|
117
|
+
field :perform_caching, type: Boolean, default: true
|
118
|
+
|
119
|
+
def cache_fragments
|
120
|
+
Hancock::Cache::Fragment.where(:name.in => cache_keys)
|
121
|
+
end
|
122
|
+
def all_cache_fragments
|
123
|
+
Hancock::Cache::Fragment.where(:name.in => all_cache_keys(false))
|
124
|
+
end
|
125
|
+
|
126
|
+
def reset_cache_keys
|
127
|
+
self.cache_keys = cache_fragments.distinct(:name)
|
128
|
+
end
|
129
|
+
def reset_cache_keys!
|
130
|
+
self.reset_cache_keys and self.save
|
131
|
+
end
|
132
|
+
def self.reset_cache_keys!
|
133
|
+
self.all.map(&:reset_cache_keys!)
|
134
|
+
end
|
135
|
+
|
136
|
+
attr_accessor :cache_cleared
|
137
|
+
|
138
|
+
after_touch :clear_cache
|
139
|
+
after_save :clear_cache
|
140
|
+
after_destroy :clear_cache
|
141
|
+
def clear_cache
|
142
|
+
if perform_caching and !cache_cleared
|
143
|
+
# (cache_keys and cache_keys.is_a?(Array) and cache_keys).compact.map(&:strip).uniq.each do |k|
|
144
|
+
(all_cache_keys and all_cache_keys.is_a?(Array) and all_cache_keys).compact.map(&:strip).uniq.each do |k|
|
145
|
+
unless k.blank?
|
146
|
+
k = k.strip
|
147
|
+
if _frag = Hancock::Cache::Fragment.where(name: k).first
|
148
|
+
_frag.clear!
|
149
|
+
else
|
150
|
+
Rails.cache.delete(k)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
# @cache_keys = nil
|
155
|
+
@all_cache_keys = nil
|
156
|
+
|
157
|
+
cache_cleared = true
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|