jason-rails 0.7.5 → 0.8.0

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: 592a7eab8028ae28815abbf0d401d98b2ceabc0009f30713f68795da21adb165
4
+ data.tar.gz: 315f5088e258dcab9755dc92f4de03f3f2f016cf6698d3619f022c1e7d601e9d
5
5
  SHA512:
6
- metadata.gz: 6959b8b6e2b97547a9c596d4efe5332339deb240c31c3951eb10e017e54932e529c851303e38bdc347c8ed89b4a209974fe790f3377128b0fb4229d14c490f66
7
- data.tar.gz: a9a17d52153d5580efcd2940437b1f5eac249f7611d38a152a4a043ea35803f1de6d2ce9e53ecd0e8dee923efd47df1be3c1b1d73e515095a4356875e25f0438
6
+ metadata.gz: a33fdc1d57462a97b7dd571222ff314cc77338e5172fcfc662f3e1fd5f008deed299abcd870900d0b50805944af822b462f6adee46953895499ce49f69df6680
7
+ data.tar.gz: 8ec1a59761856754f09bb7b6431d3d45d319daf9d4c7f030b8c7c2301c28ab2d0393952c4d7339a4d6310da2689c3cba8c71048deed931e0d3c4b0cf5c2fc2f1
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v0.8.0
2
+ - Cold cache handling:
3
+ - Expiry is now set on all cached models of one week
4
+ - Code now handles case where cached model is missing and repopulates the cache
5
+
6
+ ## v0.7.5
7
+ - Fixed JS errors when payloads arrive on websockets after the subscription has been unmounted
8
+ - Fixed errors during initialization due to calling `cache_all` on a Rails model before it had fully initialized.
9
+
1
10
  ## v0.7.3
2
11
  - 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
12
  - 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]`
data/client/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesr2323/jason",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
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.0"
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.0
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-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails