jason-rails 0.7.5.1 → 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: c0ca4e6c931fdf041e8bd13a516e11d89699396691ff6328c73a0241f5672cc9
4
- data.tar.gz: a0a6680af3dc06b0a5f96ee4f96513c172e12cf84cccc70339c0d46f7f0b742a
3
+ metadata.gz: 592a7eab8028ae28815abbf0d401d98b2ceabc0009f30713f68795da21adb165
4
+ data.tar.gz: 315f5088e258dcab9755dc92f4de03f3f2f016cf6698d3619f022c1e7d601e9d
5
5
  SHA512:
6
- metadata.gz: f39c46227135319c3c9bfe81fce8cbda84769bd9de3d14103604c95cb4106dc0ff00a362a340c2f9a981f412947116848b569d64d33c879b094c2bdd8a24a990
7
- data.tar.gz: c9cd5cf85a36769697b50f7d68c4204b6d61db2169a3378000bab6b8f1a6c05f3ecc42322c6480ea22a3dced1cbcd4625c8b64eb554ed21b3611a92f203c92fc
6
+ metadata.gz: a33fdc1d57462a97b7dd571222ff314cc77338e5172fcfc662f3e1fd5f008deed299abcd870900d0b50805944af822b462f6adee46953895499ce49f69df6680
7
+ data.tar.gz: 8ec1a59761856754f09bb7b6431d3d45d319daf9d4c7f030b8c7c2301c28ab2d0393952c4d7339a4d6310da2689c3cba8c71048deed931e0d3c4b0cf5c2fc2f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
- ## v0.7.5.1
2
- - Fix bug where axios errors get swallowed
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
3
5
 
4
6
  ## v0.7.5
5
7
  - Fixed JS errors when payloads arrive on websockets after the subscription has been unmounted
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
@@ -29,7 +29,6 @@ function createOptDis(schema, dispatch, restClient, serverActionQueue) {
29
29
  .catch(error => {
30
30
  dispatch({ type: 'jason/upsert', payload: { error } });
31
31
  serverActionQueue.itemFailed(id, error);
32
- Promise.reject(error);
33
32
  });
34
33
  }
35
34
  setInterval(dispatchServerAction, 10);
@@ -34,10 +34,7 @@ function actionCableAdapter(jasonConfig, handlePayload, dispatch, onConnected, t
34
34
  }
35
35
  function removeSubscription(config) {
36
36
  restClient_1.default.post('/jason/api/remove_subscription', { config, consumerId })
37
- .catch(e => {
38
- console.error(e);
39
- Promise.reject(e);
40
- });
37
+ .catch(e => console.error(e));
41
38
  }
42
39
  function getPayload(config, options) {
43
40
  restClient_1.default.post('/jason/api/get_payload', {
@@ -49,10 +46,7 @@ function actionCableAdapter(jasonConfig, handlePayload, dispatch, onConnected, t
49
46
  handlePayload(payload);
50
47
  });
51
48
  })
52
- .catch(e => {
53
- console.error(e);
54
- Promise.reject(e);
55
- });
49
+ .catch(e => console.error(e));
56
50
  }
57
51
  function fullChannelName(channelName) {
58
52
  return channelName;
@@ -33,19 +33,13 @@ function pusherAdapter(jasonConfig, handlePayload, dispatch) {
33
33
  configToChannel[JSON.stringify(config)] = channelName;
34
34
  subscribeToChannel(channelName);
35
35
  })
36
- .catch(e => {
37
- console.error(e);
38
- Promise.reject(e);
39
- });
36
+ .catch(e => console.error(e));
40
37
  }
41
38
  function removeSubscription(config) {
42
39
  const channelName = configToChannel[JSON.stringify(config)];
43
40
  unsubscribeFromChannel(fullChannelName(channelName));
44
41
  restClient_1.default.post('/jason/api/remove_subscription', { config, consumerId })
45
- .catch(e => {
46
- console.error(e);
47
- Promise.reject(e);
48
- });
42
+ .catch(e => console.error(e));
49
43
  }
50
44
  function getPayload(config, options) {
51
45
  restClient_1.default.post('/jason/api/get_payload', {
@@ -57,10 +51,7 @@ function pusherAdapter(jasonConfig, handlePayload, dispatch) {
57
51
  handlePayload(payload);
58
52
  });
59
53
  })
60
- .catch(e => {
61
- console.error(e);
62
- Promise.reject(e);
63
- });
54
+ .catch(e => console.error(e));
64
55
  }
65
56
  function subscribeToChannel(channelName) {
66
57
  const channel = pusher.subscribe(fullChannelName(channelName));
data/client/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesr2323/jason",
3
- "version": "0.7.5.1",
3
+ "version": "0.7.5",
4
4
  "module": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "scripts": {
@@ -29,8 +29,6 @@ export default function createOptDis(schema, dispatch, restClient, serverActionQ
29
29
  .catch(error => {
30
30
  dispatch({ type: 'jason/upsert', payload: { error } })
31
31
  serverActionQueue.itemFailed(id, error)
32
-
33
- Promise.reject(error)
34
32
  })
35
33
  }
36
34
 
@@ -35,10 +35,7 @@ export default function actionCableAdapter(jasonConfig, handlePayload, dispatch,
35
35
 
36
36
  function removeSubscription(config) {
37
37
  restClient.post('/jason/api/remove_subscription', { config, consumerId })
38
- .catch(e => {
39
- console.error(e)
40
- Promise.reject(e)
41
- })
38
+ .catch(e => console.error(e))
42
39
  }
43
40
 
44
41
  function getPayload(config, options) {
@@ -51,10 +48,7 @@ export default function actionCableAdapter(jasonConfig, handlePayload, dispatch,
51
48
  handlePayload(payload)
52
49
  })
53
50
  })
54
- .catch(e => {
55
- console.error(e)
56
- Promise.reject(e)
57
- })
51
+ .catch(e => console.error(e))
58
52
  }
59
53
 
60
54
  function fullChannelName(channelName) {
@@ -31,20 +31,14 @@ export default function pusherAdapter(jasonConfig, handlePayload, dispatch) {
31
31
  configToChannel[JSON.stringify(config)] = channelName
32
32
  subscribeToChannel(channelName)
33
33
  })
34
- .catch(e => {
35
- console.error(e)
36
- Promise.reject(e)
37
- })
34
+ .catch(e => console.error(e))
38
35
  }
39
36
 
40
37
  function removeSubscription(config) {
41
38
  const channelName = configToChannel[JSON.stringify(config)]
42
39
  unsubscribeFromChannel(fullChannelName(channelName))
43
40
  restClient.post('/jason/api/remove_subscription', { config, consumerId })
44
- .catch(e => {
45
- console.error(e)
46
- Promise.reject(e)
47
- })
41
+ .catch(e => console.error(e))
48
42
  }
49
43
 
50
44
  function getPayload(config, options) {
@@ -57,10 +51,7 @@ export default function pusherAdapter(jasonConfig, handlePayload, dispatch) {
57
51
  handlePayload(payload)
58
52
  })
59
53
  })
60
- .catch(e => {
61
- console.error(e)
62
- Promise.reject(e)
63
- })
54
+ .catch(e => console.error(e))
64
55
  }
65
56
 
66
57
  function subscribeToChannel(channelName) {
@@ -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.1"
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.1
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: 2022-04-12 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