card-mod-solid_cache 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0b322e3029fa67070e6c4cba4bfe4713367188fc3b3322010df453960776d2b8
4
+ data.tar.gz: 5ec067371aac1ce2ee721bd011a788f93f5aa790c5bdd0689f284dc040fe9e2c
5
+ SHA512:
6
+ metadata.gz: f6feb7d1863300120d5980f60fac939b3c2b83da0b20169afdc5cc156d31f81494a5351797f5161fbf4ef506b518a1f13d571266ff36e8d979f9f696e2886cef
7
+ data.tar.gz: 31e92670dcb54408bbd6b1d828fe0c12496b5a22378314bc6d1c028327eec22d196d3a8207e85faf5a474195345a9bbdba4cadbe87c8f1d8575c79f4d8752855
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ <!--
2
+ # @title README - mod: solid cache
3
+ -->
4
+
5
+ # Solid Cache mod (experimental)
6
+
7
+ This mod supports caching rendered card content in the database.
8
+
9
+ ## Why
10
+
11
+ Decko has a powerful caching system that handles caching code objects
12
+ (especially cards) and rendered views. The view caching is smart about nests and
13
+ generally caches nested content separately from the content that nests it, so
14
+ that you don't have to expire the cache of the nesting content so frequently. In
15
+ most cases, this is a fast and efficient way to handle content.
16
+
17
+ However, there are cases where a deeper and more permanent cache is appropriate.
18
+ Sometimes it's more important that a very complex page be fast than perfectly up
19
+ to date, for example. This mod can be helpful in such cases.
20
+
21
+ ## How
22
+
23
+ If you include `Abstract::SolidCache` in a set, then anything that renders the
24
+ `:core` view of a card in that set will trigger the generation of a
25
+ `+:solid_cache` card. That raw content (db_content) of that new cache card will
26
+ contain the rendered core view of the original card. Any further rendering of
27
+ the core view will use that cached view until the cache is explicitly cleared or
28
+ updated.
29
+
30
+ To clear or update the cache, you will need to call one of the following methods
31
+ to the set of a card that should clear/update the cache when changed.
32
+
33
+ - `#cache_update_trigger`
34
+ - `#cache_expire_trigger`
35
+
36
+ See {Card::Set::Abstract::SolidCache} for more on those methods.
@@ -0,0 +1,114 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ # A card that includes Abstract::SolidCache has its "core" view fully rendered
4
+ # and stored in a '+*solid cache' card.
5
+ # If that card exists the core view returns its content as rendered view.
6
+ # If it doesn't exist the usual core view is rendered and saved in that card.
7
+ #
8
+ # The cache expiration can be controlled with the cache_update_trigger and
9
+ # cache_expire_trigger methods.
10
+
11
+ card_accessor :solid_cache, type: HtmlID
12
+
13
+ class << self
14
+ def included host_class
15
+ host_class.format(host_class.try(:cached_format) || :base) do
16
+ view :core, cache: :never do
17
+ return super() if voo.hide? :solid_cache
18
+ _render_solid_cache
19
+ end
20
+
21
+ view :solid_cache, cache: :never do
22
+ card.with_solid_cache do |cache_card|
23
+ subformat(cache_card)._render_core
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ module ClassMethods
31
+ # If a card of the set given by 'set_of_changed_card' is changed
32
+ # the given block is executed. It is supposed to return an array of
33
+ # cards whose solid caches are expired because of the change.
34
+ # @param set_of_changed_card [set constant] a set of cards that triggers
35
+ # a cache update
36
+ # @param args [Hash]
37
+ # @option args [Symbol, Array<Symbol>] :on the action(s)
38
+ # (:create, :update, or :delete) on which the cache update
39
+ # should be triggered. Default is all actions.
40
+ # @option args [Stage] :in_stage the stage when the update is executed.
41
+ # Default is :integrate
42
+ # @yield return a card or an array of cards with solid cache that need to be
43
+ # updated
44
+ def cache_update_trigger set_of_changed_card, args={}, &block
45
+ define_event_to_update_expired_cached_cards(
46
+ set_of_changed_card, args, :update_solid_cache, &block
47
+ )
48
+ end
49
+
50
+ # Same as 'cache_update_trigger' but expires instead of updates the
51
+ # outdated solid caches
52
+ def cache_expire_trigger set_of_changed_card, args={}, &block
53
+ define_event_to_update_expired_cached_cards(
54
+ set_of_changed_card, args, :expire_solid_cache, &block
55
+ )
56
+ end
57
+
58
+ private
59
+
60
+ def define_event_to_update_expired_cached_cards set_of_changed_card, args,
61
+ method_name
62
+ args[:on] ||= %i[create update delete]
63
+ name = event_name set_of_changed_card, args
64
+ stage = args[:in_stage] || :integrate
65
+ Card::Set.register_set set_of_changed_card
66
+ set_of_changed_card.event name, stage, args do
67
+ Array.wrap(yield(self)).compact.each do |expired_cache_card|
68
+ next unless expired_cache_card.solid_cache?
69
+ expired_cache_card.send method_name
70
+ end
71
+ end
72
+ end
73
+
74
+ def event_name set, args
75
+ changed_card_set = set.underscore
76
+ solid_cache_set = underscore + "__solid_cache"
77
+ actions = Array.wrap(args[:on]).join("_")
78
+ ["update", solid_cache_set,
79
+ "changed_by", changed_card_set,
80
+ "on", actions].join("___").to_sym
81
+ end
82
+ end
83
+
84
+ def solid_cache?
85
+ true
86
+ end
87
+
88
+ def with_solid_cache
89
+ update_solid_cache if solid_cache_card.new?
90
+ yield solid_cache_card
91
+ end
92
+
93
+ def expire_solid_cache _changed_card=nil
94
+ return unless solid_cache? && solid_cache_card.real?
95
+ Auth.as_bot do
96
+ solid_cache_card.delete!
97
+ end
98
+ end
99
+
100
+ def update_solid_cache
101
+ return unless solid_cache?
102
+ new_content = generate_content_for_cache
103
+ write_to_solid_cache new_content
104
+ new_content
105
+ end
106
+
107
+ def generate_content_for_cache
108
+ format_type = try(:cached_format) || :base
109
+ format(format_type)._render_core hide: :solid_cache
110
+ end
111
+
112
+ def write_to_solid_cache new_content
113
+ solid_cache_card.write! new_content
114
+ end
@@ -0,0 +1,15 @@
1
+ # for override
2
+ def solid_cache?
3
+ false
4
+ end
5
+
6
+ module ClassMethods
7
+ def clear_solid_cache
8
+ Auth.as_bot do
9
+ Card.search(right: { codename: "solid_cache" }).each do |card|
10
+ card.update_columns trash: true
11
+ card.expire
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,48 @@
1
+ include_set Abstract::Lock
2
+
3
+ def ok_to_read
4
+ left.ok_to_read
5
+ end
6
+
7
+ def followable?
8
+ false
9
+ end
10
+
11
+ def history?
12
+ false
13
+ end
14
+
15
+ def clean_html?
16
+ false
17
+ end
18
+
19
+ def write! new_content
20
+ lock do
21
+ if new_card?
22
+ update! content: new_content
23
+ elsif new_content != solid_cache_card.content
24
+ update_column :db_content, new_content
25
+ expire
26
+ end
27
+ end
28
+ end
29
+
30
+ format :html do
31
+ view :core, cache: :never do
32
+ return super() unless card.new_card?
33
+ @denied_view = :core
34
+ _render_unknown
35
+ end
36
+
37
+ view :unknown, cache: :never do
38
+ if @card.new_card? && (l = @card.left) && l.solid_cache?
39
+ l.update_solid_cache
40
+ @card = Card.fetch card.name
41
+ render! @denied_view
42
+ else
43
+ super()
44
+ end
45
+ end
46
+
47
+ view :new, :unknown
48
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card-mod-solid_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Philipp Kühl
8
+ - Ethan McCutchen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: card
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ description: ''
29
+ email:
30
+ - info@decko.org
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - set/abstract/solid_cache.rb
37
+ - set/all/solid_cache.rb
38
+ - set/right/solid_cache.rb
39
+ homepage: http://decko.org
40
+ licenses:
41
+ - GPL-3.0
42
+ metadata:
43
+ card-mod: solid_cache
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '2.5'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.2.28
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: solid cache
63
+ test_files: []