gcra 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: d9436b4bda7bf469a2a3cf08df00716039ba14ef
4
- data.tar.gz: d3b31ffe07a84f62310acb0724ee7401be104ab1
3
+ metadata.gz: e37214a0a8181beff8863287ffdabee12b6cd6a4
4
+ data.tar.gz: d23082af239177bafeda35a8f4db1d0223d0d09a
5
5
  SHA512:
6
- metadata.gz: f50866ecc292bb86a613e421b4c095a8e1a7afe452c292c63242e3e912b99744266e2f8621ca087cdb85619d6fc20772a66fbf3065082e440c4dd452540c210e
7
- data.tar.gz: d820ff9d540b75d471392e3a56dcab43b700c21a05ca60ec317926b945a67ce45fa905159385431432d153692127063393169aed5d693f32fef4e9c3ef630d58
6
+ metadata.gz: 75d41403c38028215e7afd7962ade25cb24c751e99ae349ee02c4a17d2f8ad26fb5510d81d108a70b32900aafec0161c588cf6b492c00a0995c099b34bc85c08
7
+ data.tar.gz: 945835400bcf67bbaa8e153c12fb612a08a626e59cd9cbc6b5d33045dfe43a627d80106113db847bc2855f00ab5de2ee2932421a48d489aef90d26c014bab53e
@@ -9,11 +9,7 @@ module GCRA
9
9
  if v ~= ARGV[1] then
10
10
  return 0
11
11
  end
12
- if ARGV[3] ~= "0" then
13
- redis.call('psetex', KEYS[1], ARGV[3], ARGV[2])
14
- else
15
- redis.call('set', KEYS[1], ARGV[2])
16
- end
12
+ redis.call('psetex', KEYS[1], ARGV[3], ARGV[2])
17
13
  return 1
18
14
  EOF
19
15
  CAS_SCRIPT_MISSING_KEY_RESPONSE = 'key does not exist'.freeze
@@ -43,8 +39,8 @@ module GCRA
43
39
  full_key = @key_prefix + key
44
40
  did_set = @redis.setnx(full_key, value)
45
41
 
46
- if did_set && ttl_nano > 0
47
- ttl_milli = ttl_nano / 1_000_000
42
+ if did_set
43
+ ttl_milli = calculate_ttl_milli(ttl_nano)
48
44
  @redis.pexpire(full_key, ttl_milli)
49
45
  end
50
46
 
@@ -57,7 +53,7 @@ module GCRA
57
53
  def compare_and_set_with_ttl(key, old_value, new_value, ttl_nano)
58
54
  full_key = @key_prefix + key
59
55
  begin
60
- ttl_milli = ttl_nano / 1_000_000
56
+ ttl_milli = calculate_ttl_milli(ttl_nano)
61
57
  swapped = @redis.eval(CAS_SCRIPT, keys: [full_key], argv: [old_value, new_value, ttl_milli])
62
58
  rescue Redis::CommandError => e
63
59
  if e.message == CAS_SCRIPT_MISSING_KEY_RESPONSE
@@ -68,5 +64,17 @@ module GCRA
68
64
 
69
65
  return swapped == 1
70
66
  end
67
+
68
+ private
69
+
70
+ def calculate_ttl_milli(ttl_nano)
71
+ ttl_milli = ttl_nano / 1_000_000
72
+ # Setting 0 as expiration/ttl would result in an error.
73
+ # Therefore overwrite it and use 1
74
+ if ttl_milli == 0
75
+ return 1
76
+ end
77
+ return ttl_milli
78
+ end
71
79
  end
72
80
  end
data/lib/gcra/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module GCRA
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcra
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Frister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-08 00:00:00.000000000 Z
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec