Module: Polyrun::Data::CachedFixtures
- Defined in:
- lib/polyrun/data/cached_fixtures.rb
Overview
Process-local memoization for expensive fixture setup (register / cached). Use inside before(:suite) or a support file so each parallel process builds once; not for threads without external locking (see ParallelProvisioning).
Example:
Polyrun::Data::CachedFixtures.register(:admin) { User.create!(email: "a@example.com") }
Polyrun::Data::CachedFixtures.fetch(:admin) # => same object
Defined Under Namespace
Classes: Cache
Class Method Summary collapse
- .after_reset(&block) ⇒ Object
- .before_reset(&block) ⇒ Object
- .cached(id) ⇒ Object
- .disable! ⇒ Object
- .disabled? ⇒ Boolean
- .enable! ⇒ Object
- .fetch(id, &block) ⇒ Object (also: register)
- .format_stats_report(title: "Polyrun cached fixtures") ⇒ Object
- .reset! ⇒ Object
- .stats ⇒ Object
Class Method Details
.after_reset(&block) ⇒ Object
92 93 94 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 92 def after_reset(&block) REGISTRY.callbacks[:after_reset] << block if block end |
.before_reset(&block) ⇒ Object
88 89 90 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 88 def before_reset(&block) REGISTRY.callbacks[:before_reset] << block if block end |
.cached(id) ⇒ Object
74 75 76 77 78 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 74 def cached(id) return unless REGISTRY.cache.store.key?(id.to_s) REGISTRY.cache.store[id.to_s] end |
.disable! ⇒ Object
96 97 98 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 96 def disable! REGISTRY.disabled = true end |
.disabled? ⇒ Boolean
104 105 106 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 104 def disabled? REGISTRY.disabled == true end |
.enable! ⇒ Object
100 101 102 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 100 def enable! REGISTRY.disabled = false end |
.fetch(id, &block) ⇒ Object Also known as: register
66 67 68 69 70 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 66 def fetch(id, &block) return yield if disabled? REGISTRY.cache.fetch(id.to_s, &block) end |
.format_stats_report(title: "Polyrun cached fixtures") ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 112 def format_stats_report(title: "Polyrun cached fixtures") lines = [title] REGISTRY.cache.stats_snapshot.each do |key, s| lines << format(" %-40s build: %0.4fs hits: %d", key, s[:build_time], s[:hits]) end lines.join("\n") + "\n" end |
.reset! ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 80 def reset! REGISTRY.callbacks[:before_reset].each(&:call) REGISTRY.cache.clear REGISTRY.callbacks[:after_reset].each(&:call) REGISTRY.callbacks[:before_reset].clear REGISTRY.callbacks[:after_reset].clear end |
.stats ⇒ Object
108 109 110 |
# File 'lib/polyrun/data/cached_fixtures.rb', line 108 def stats REGISTRY.cache.stats_snapshot end |