jason-rails 0.7.5 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b901c6b15b2b4f00d307c0e86811d660a68be05ac8c43f3d48c41412a020b4e8
4
- data.tar.gz: 3fbed93cb66f5acffe86a8254f0be3ed67979bf93d72ab8faed77a0ae552fff3
3
+ metadata.gz: 2feafe450ab2dbdf8716f672f67b89edd8792412c7c2ab56b6f44e68171bb5f3
4
+ data.tar.gz: cce30e46ce1c392f836260c9f0b0ee6fc35fe7e98d1257f933afc434aa509299
5
5
  SHA512:
6
- metadata.gz: 6959b8b6e2b97547a9c596d4efe5332339deb240c31c3951eb10e017e54932e529c851303e38bdc347c8ed89b4a209974fe790f3377128b0fb4229d14c490f66
7
- data.tar.gz: a9a17d52153d5580efcd2940437b1f5eac249f7611d38a152a4a043ea35803f1de6d2ce9e53ecd0e8dee923efd47df1be3c1b1d73e515095a4356875e25f0438
6
+ metadata.gz: f1e13ed682fbd187ccd5f196192a6374a205da2d75f43e79a7653a4db1bd089c65f1a1d72939d3358e916802714814fdf3d4bc32c12760102b4f87fa22c8f6a9
7
+ data.tar.gz: e26a2acf832d9ae9b72ab299fb705d95680bf8c8d91f5c9b44e6e9ffb639ac6010a4e9460692e21a06500a46989e22e04e7032fc1b22a088d15d896b9e6f569a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## v0.8.1
2
+ - Allow setting the Sidekiq queue name for Jason payloads
3
+
4
+ ## v0.8.0
5
+ - Cold cache handling:
6
+ - Expiry is now set on all cached models of one week
7
+ - Code now handles case where cached model is missing and repopulates the cache
8
+
9
+ ## v0.7.5
10
+ - Fixed JS errors when payloads arrive on websockets after the subscription has been unmounted
11
+ - Fixed errors during initialization due to calling `cache_all` on a Rails model before it had fully initialized.
12
+
1
13
  ## v0.7.3
2
14
  - Added: Add JasonContext to exports, for use in scenarios where you need to forward the context into some other React reconciler (e.g. `react-three-fiber`)
3
15
  - Fixed: Unneeded `reload` in Publisher resulting in extra database calls
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jason-rails (0.7.1)
4
+ jason-rails (0.7.5)
5
5
  connection_pool (>= 2.2.3)
6
6
  jsondiff
7
7
  rails (>= 5)
data/README.md CHANGED
@@ -21,7 +21,7 @@ An alternative way of thinking about Jason is "what if we applied the Flux/Redux
21
21
  Add the gem and the NPM package
22
22
 
23
23
  ```ruby
24
- gem 'jason-rails'
24
+ gem 'jason-rails', require: 'jason'
25
25
  ```
26
26
 
27
27
  ```bash
@@ -180,6 +180,7 @@ Development is primarily driven by the needs of projects we're using Jason in. I
180
180
 
181
181
  ## Publishing a new version
182
182
  - Update `version.rb`
183
+ - Update CHANGELOG
183
184
  - `gem build`
184
185
  - `gem push`
185
186
  - `npm version [major/minor/patch]`
@@ -1,5 +1,8 @@
1
1
  class Jason::OutboundMessageQueueWorker
2
- include Sidekiq::Worker if defined?(Sidekiq) # Even if not using Pusher, this gets autoloaded
2
+ if defined?(Sidekiq) # Even if not using Pusher, this gets autoloaded
3
+ include Sidekiq::Worker
4
+ sidekiq_options queue: Jason.sidekiq_queue
5
+ end
3
6
 
4
7
  def perform
5
8
  batch = get_batch
data/client/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesr2323/jason",
3
- "version": "0.7.4",
3
+ "version": "0.8.0",
4
4
  "module": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "scripts": {
@@ -1,10 +1,13 @@
1
1
  class Jason::LuaGenerator
2
2
  ## TODO load these scripts and evalsha
3
3
  def cache_json(model_name, id, payload)
4
+ expiry = 7*24*60*60 + rand(6*60*60)
5
+
6
+ # ensure the content expires first
4
7
  cmd = <<~LUA
5
8
  local gidx = redis.call('INCR', 'jason:gidx')
6
- redis.call( 'set', 'jason:cache:' .. ARGV[1] .. ':' .. ARGV[2] .. ':gidx', gidx )
7
- redis.call( 'hset', 'jason:cache:' .. ARGV[1], ARGV[2], ARGV[3] )
9
+ redis.call( 'setex', 'jason:cache:' .. ARGV[1] .. ':' .. ARGV[2] .. ':gidx', #{expiry}, gidx )
10
+ redis.call( 'setex', 'jason:cache:' .. ARGV[1] .. ':' .. ARGV[2], #{expiry - 60}, ARGV[3] )
8
11
  return gidx
9
12
  LUA
10
13
 
@@ -15,15 +18,26 @@ class Jason::LuaGenerator
15
18
  # If value has changed, return old value and new idx. Otherwise do nothing.
16
19
  cmd = <<~LUA
17
20
  local t = {}
18
- local models = {}
21
+ local insts = {}
22
+ local miss_ids = {}
19
23
  local ids = redis.call('smembers', 'jason:subscriptions:' .. ARGV[2] .. ':ids:' .. ARGV[1])
20
24
 
21
25
  for k,id in pairs(ids) do
22
- models[#models+1] = redis.call( 'hget', 'jason:cache:' .. ARGV[1], id)
26
+ local result = redis.call( 'get', 'jason:cache:' .. ARGV[1] .. ':' .. id)
27
+ if (result == false) then
28
+ miss_ids[#miss_ids+1] = id
29
+ else
30
+ insts[#insts+1] = result
31
+ end
23
32
  end
24
33
 
25
- t[#t+1] = models
26
- t[#t+1] = redis.call( 'get', 'jason:subscription:' .. ARGV[2] .. ':' .. ARGV[1] .. ':idx' )
34
+ if next(miss_ids) == nil then
35
+ t[#t+1] = insts
36
+ t[#t+1] = redis.call( 'get', 'jason:subscription:' .. ARGV[2] .. ':' .. ARGV[1] .. ':idx' )
37
+ else
38
+ t[#t+1] = miss_ids
39
+ t[#t+1] = 'missing'
40
+ end
27
41
 
28
42
  return t
29
43
  LUA
@@ -124,7 +124,7 @@ module Jason::Publisher
124
124
  end
125
125
 
126
126
  def jason_cached_value
127
- JSON.parse($redis_jason.hget("jason:cache:#{self.class.name.underscore}", id) || '{}')
127
+ JSON.parse($redis_jason.get("jason:cache:#{self.class.name.underscore}:#{id}") || '{}')
128
128
  end
129
129
 
130
130
  class_methods do
@@ -132,6 +132,10 @@ module Jason::Publisher
132
132
  all.find_each(&:cache_json)
133
133
  end
134
134
 
135
+ def cache_for(ids)
136
+ where(id: ids).find_each(&:cache_json)
137
+ end
138
+
135
139
  def has_jason?
136
140
  true
137
141
  end
@@ -399,16 +399,17 @@ class Jason::Subscription
399
399
  end
400
400
 
401
401
  def get_for_model(model_name)
402
- if $redis_jason.sismember("jason:models:#{model_name}:all:subscriptions", id)
403
- instance_jsons_hash, idx = $redis_jason.multi do |r|
404
- r.hgetall("jason:cache:#{model_name}")
405
- r.get("jason:subscription:#{id}:#{model_name}:idx")
406
- end
407
- instance_jsons = instance_jsons_hash.values
408
- else
402
+ instance_jsons, idx = Jason::LuaGenerator.new.get_payload(model_name, id)
403
+ if idx == 'missing'
404
+ # warm cache and then retry
405
+ model_klass(model_name).cache_for(instance_jsons)
409
406
  instance_jsons, idx = Jason::LuaGenerator.new.get_payload(model_name, id)
410
407
  end
411
408
 
409
+ if instance_jsons.any? { |json| json.blank? }
410
+ raise Jason::MissingCacheError
411
+ end
412
+
412
413
  payload = instance_jsons.map do |instance_json|
413
414
  instance_json ? JSON.parse(instance_json) : {}
414
415
  end
@@ -437,7 +438,7 @@ class Jason::Subscription
437
438
 
438
439
  def add(model_name, instance_id)
439
440
  idx = $redis_jason.incr("jason:subscription:#{id}:#{model_name}:idx")
440
- payload = JSON.parse($redis_jason.hget("jason:cache:#{model_name}", instance_id) || '{}')
441
+ payload = JSON.parse($redis_jason.get("jason:cache:#{model_name}:#{instance_id}") || '{}')
441
442
 
442
443
  payload = {
443
444
  id: instance_id,
@@ -479,3 +480,5 @@ class Jason::Subscription
479
480
  broadcaster.broadcast(payload)
480
481
  end
481
482
  end
483
+
484
+ class Jason::MissingCacheError < StandardError; end
data/lib/jason/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jason
2
- VERSION = "0.7.5"
2
+ VERSION = "0.8.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jason-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Rees
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-23 00:00:00.000000000 Z
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails