svelte-on-rails 22.2.0 → 22.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a32126c57f42015b2fae36849a77266d174dfc6234a5958c924b490e4c1efc47
4
- data.tar.gz: b264887e056b7d3aeb763923d1f20eccdab81000bc1c7dae63b7c909397157c0
3
+ metadata.gz: a95b04eb2bc4fe9f3b9cf489af8550377bb35bd8297b7c8c9849fae0b4985e89
4
+ data.tar.gz: d648e0c9240757feb39c42904165bbd985ee24c19de78e2b1d5068c60c2b9539
5
5
  SHA512:
6
- metadata.gz: ec6ad182cd5a2f06c654017f2ddbc81fe07b07498339262b0ec145d7fbaa815d12abef5a681930080d8edaee57e536f036399e5df011c8e3d3413ecea9b0a5f7
7
- data.tar.gz: e67beb89b3a4ed77bdb3d7cc39f54596769c1b9cd25d12b7db7341cf860176d0b9a633b6fba4e4703a2a48d42509a1b18a977345b0a91214cefe556c760766b2
6
+ metadata.gz: f32d6e15c061f5c95abaffdf1e74ac295bd89058a9eb226e8ead4ac34321890509f0dddfe2af0e3e8c5a0e0d0696ac4236eb224c41928ca1ed2cd2f06ac51aca
7
+ data.tar.gz: 588239e64c3a43f660c3099710d0b0a41f59fc0636be72d7458756a8b55fd6e773fae911937de0099d5b7d4d30466d2856a62b0405f5e2a6ca2e19fe23e208b7
@@ -51,6 +51,7 @@ module SvelteOnRails
51
51
 
52
52
  rake_tasks do
53
53
  load File.expand_path("../../tasks/svelte_on_rails_tasks.rake", __FILE__)
54
+ load File.expand_path("../../tasks/cache_tasks.rake", __FILE__)
54
55
  end
55
56
 
56
57
  end
@@ -0,0 +1,156 @@
1
+ namespace :svelte_on_rails do
2
+ namespace :cache do
3
+
4
+ desc "Delete all svelte-on-rails cache keys from Redis (scoped to the configured redis_namespace)"
5
+ task clear: :environment do
6
+ conf = SvelteOnRails::Configuration.instance
7
+ redis = conf.redis_instance
8
+ namespace = conf.redis_namespace
9
+
10
+ unless redis
11
+ warn "svelte_on_rails:clear_cache — no redis_instance configured, nothing to do."
12
+ next
13
+ end
14
+
15
+ if Rails.env.production? && ENV["FORCE"] != "1"
16
+ print "You are about to delete all keys matching «#{namespace}:*» in PRODUCTION. Continue? (y/N) "
17
+ answer = STDIN.gets.to_s.strip.downcase
18
+ unless %w[y yes].include?(answer)
19
+ puts "Aborted."
20
+ next
21
+ end
22
+ end
23
+
24
+ pattern = "#{namespace}:*"
25
+ cursor = "0"
26
+ total_deleted = 0
27
+ batches = 0
28
+
29
+ puts "Scanning Redis for keys matching «#{pattern}» ..."
30
+
31
+ loop do
32
+ cursor, keys = redis.scan(cursor, match: pattern, count: 500)
33
+ if keys.any?
34
+ # UNLINK is non-blocking on Redis >= 4; fall back to DEL if not available.
35
+ deleted = begin
36
+ redis.unlink(*keys)
37
+ rescue Redis::CommandError
38
+ redis.del(*keys)
39
+ end
40
+ total_deleted += deleted
41
+ batches += 1
42
+ end
43
+ break if cursor == "0"
44
+ rescue => e
45
+ warn "svelte_on_rails:clear_cache — error during scan/delete: #{e.class}: #{e.message}"
46
+ break
47
+ end
48
+
49
+ puts "svelte_on_rails:clear_cache — deleted #{total_deleted} key(s) in #{batches} batch(es) under «#{pattern}»."
50
+ end
51
+
52
+ desc "Show all svelte-on-rails Redis keys with their memory footprint (scoped to redis_namespace). Env: LIMIT=n, SORT=size|key, PATTERN=extra-glob"
53
+ task show: :environment do
54
+ conf = SvelteOnRails::Configuration.instance
55
+ redis = conf.redis_instance
56
+ namespace = conf.redis_namespace
57
+
58
+ unless redis
59
+ warn "svelte_on_rails:show_cache — no redis_instance configured, nothing to show."
60
+ next
61
+ end
62
+
63
+ extra = ENV["PATTERN"].to_s.strip
64
+ pattern = extra.empty? ? "#{namespace}:*" : "#{namespace}:*#{extra}*"
65
+ limit = ENV["LIMIT"].to_s =~ /\A\d+\z/ ? ENV["LIMIT"].to_i : nil
66
+ sort_by = %w[size key].include?(ENV["SORT"]) ? ENV["SORT"] : "size"
67
+
68
+ puts "Scanning Redis for keys matching «#{pattern}» ..."
69
+
70
+ entries = [] # [{ key:, bytes:, ttl:, type: }]
71
+ cursor = "0"
72
+
73
+ loop do
74
+ cursor, keys = redis.scan(cursor, match: pattern, count: 500)
75
+
76
+ if keys.any?
77
+ # Pipeline MEMORY USAGE / TTL / TYPE for the whole batch.
78
+ results = redis.pipelined do |p|
79
+ keys.each do |k|
80
+ p.call(["MEMORY", "USAGE", k])
81
+ p.ttl(k)
82
+ p.type(k)
83
+ end
84
+ end
85
+
86
+ keys.each_with_index do |k, i|
87
+ bytes = results[i * 3]
88
+ ttl = results[i * 3 + 1]
89
+ type = results[i * 3 + 2]
90
+ entries << { key: k, bytes: bytes.to_i, ttl: ttl.to_i, type: type }
91
+ end
92
+ end
93
+
94
+ break if cursor == "0"
95
+ rescue => e
96
+ warn "svelte_on_rails:show_cache — error during scan: #{e.class}: #{e.message}"
97
+ break
98
+ end
99
+
100
+ if entries.empty?
101
+ puts "No keys found under «#{pattern}»."
102
+ next
103
+ end
104
+
105
+ entries.sort_by! { |e| sort_by == "size" ? -e[:bytes] : e[:key] }
106
+ shown = limit ? entries.first(limit) : entries
107
+
108
+ key_w = [shown.map { |e| e[:key].length }.max, 3].max
109
+ size_w = [shown.map { |e| human_bytes(e[:bytes]).length }.max, 4].max
110
+ ttl_w = [shown.map { |e| ttl_str(e[:ttl]).length }.max, 3].max
111
+ type_w = [shown.map { |e| e[:type].to_s.length }.max, 4].max
112
+
113
+ header = format("%-#{key_w}s %#{size_w}s %#{ttl_w}s %-#{type_w}s", "KEY", "SIZE", "TTL", "TYPE")
114
+ puts header
115
+ puts "-" * header.length
116
+
117
+ shown.each do |e|
118
+ puts format(
119
+ "%-#{key_w}s %#{size_w}s %#{ttl_w}s %-#{type_w}s",
120
+ e[:key], human_bytes(e[:bytes]), ttl_str(e[:ttl]), e[:type]
121
+ )
122
+ end
123
+
124
+ total_bytes = entries.sum { |e| e[:bytes] }
125
+ puts "-" * header.length
126
+ puts "Total: #{entries.size} key(s), #{human_bytes(total_bytes)} in RAM" \
127
+ "#{limit && entries.size > limit ? " (showing top #{limit} by #{sort_by})" : ""}"
128
+ end
129
+ end
130
+ end
131
+
132
+ # Helpers used by svelte_on_rails:show_cache
133
+ def human_bytes(n)
134
+ return "0 B" if n.nil? || n <= 0
135
+ units = %w[B KB MB GB TB]
136
+ i = (Math.log(n) / Math.log(1024)).floor
137
+ i = units.length - 1 if i >= units.length
138
+ format("%.2f %s", n.to_f / (1024 ** i), units[i])
139
+ end
140
+
141
+ def ttl_str(ttl)
142
+ case ttl
143
+ when -2 then "gone"
144
+ when -1 then "∞"
145
+ else
146
+ if ttl >= 86_400 then
147
+ "#{(ttl / 86_400.0).round(1)}d"
148
+ elsif ttl >= 3_600 then
149
+ "#{(ttl / 3_600.0).round(1)}h"
150
+ elsif ttl >= 60 then
151
+ "#{(ttl / 60.0).round(1)}m"
152
+ else
153
+ "#{ttl}s"
154
+ end
155
+ end
156
+ end
@@ -157,4 +157,4 @@ namespace :svelte_on_rails do
157
157
 
158
158
  end
159
159
 
160
- Rake::Task["assets:precompile"].enhance ["svelte_on_rails:precompile"]
160
+ Rake::Task["assets:precompile"].enhance ["svelte_on_rails:precompile"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 22.2.0
4
+ version: 22.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair
@@ -100,6 +100,7 @@ files:
100
100
  - lib/svelte_on_rails/ssr_client.rb
101
101
  - lib/svelte_on_rails/turbo_stream.rb
102
102
  - lib/svelte_on_rails/view_helpers.rb
103
+ - lib/tasks/cache_tasks.rake
103
104
  - lib/tasks/svelte_on_rails_tasks.rake
104
105
  - templates/config_base/app/frontend/ssr/ssr.js
105
106
  - templates/config_base/config/svelte_on_rails.yml