atomic_cache 0.4.0.rc1 → 0.4.1.rc1

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: 0f8d7906f63f08bf7e8d7b9f9f88d05a209d65c37375c464f60448ffd9db427e
4
- data.tar.gz: ac2b7c09f3299ccbb4dcf588cbe163134c8e81b94d79c08fe908afb6200911a7
3
+ metadata.gz: db2b157ed872e6cb90af8cf2ef61a97f237485bc763998464eb4faf488d26c11
4
+ data.tar.gz: 48e13e212479454f2ece192c81c8f755a4aa039a2cf5930cf1cdfea2eaae438d
5
5
  SHA512:
6
- metadata.gz: fa4f82e7cd8b729461b5b122a9f9cb92c2c36898c295a63bdd18a510133920ca5694d3e44b9cceceb7d5e2a078b2781f75481b32bda1e21804f720a00287eacb
7
- data.tar.gz: 18ca04880e1f4c57308d3442fa4bbb50318121ba9d663ca2858819838ddcd27c118ad79b4767c60708872778deca668b7315f7cad32fdcb951cec2a18dc168ac
6
+ metadata.gz: 5fb9aba2eaeb20e9e3206c80039941b005c7f6e82c05bfddd59482783b028706f706a6843a8ea8261d18895732035d1deeabda178e5437620f33ca9abf88b54e
7
+ data.tar.gz: 06511e8a0ec9b33de005994f6938877f85b053c03072f52b6cc0dbd22a99389dfb36245f7002d59ea364d1ea583eec0b440c6bd2a5e1b84604434474275d6fe2
@@ -16,7 +16,7 @@ module AtomicCache
16
16
 
17
17
  def add(raw_key, new_value, ttl, user_options={})
18
18
  store_op(raw_key, user_options) do |key, options|
19
- return false if store.has_key?(key)
19
+ return false if store.has_key?(key) && !ttl_expired?(store[key])
20
20
  write(key, new_value, ttl, user_options)
21
21
  end
22
22
  end
@@ -29,8 +29,7 @@ module AtomicCache
29
29
  unmarshaled = unmarshal(entry[:value], user_options)
30
30
  return unmarshaled if entry[:ttl].nil? or entry[:ttl] == false
31
31
 
32
- life = Time.now - entry[:written_at]
33
- if (life >= entry[:ttl])
32
+ if ttl_expired?(entry)
34
33
  store.delete(key)
35
34
  nil
36
35
  else
@@ -54,6 +53,12 @@ module AtomicCache
54
53
 
55
54
  protected
56
55
 
56
+ def ttl_expired?(entry)
57
+ return false unless entry
58
+ life = Time.now - entry[:written_at]
59
+ life >= entry[:ttl]
60
+ end
61
+
57
62
  def write(key, value, ttl=nil, user_options)
58
63
  store[key] = {
59
64
  value: marshal(value, user_options),
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AtomicCache
4
- VERSION = "0.4.0.rc1"
4
+ VERSION = "0.4.1.rc1"
5
5
  end
@@ -17,17 +17,34 @@ shared_examples 'memory storage' do
17
17
  expect(result).to eq(true)
18
18
  end
19
19
 
20
- it 'does not write the key if it exists' do
21
- entry = { value: Marshal.dump('foo'), ttl: 100, written_at: 100 }
20
+ # SharedMemory.new.add("foo", ttl: 100)
21
+
22
+ it 'does not write the key if it exists but expiration time is NOT up' do
23
+ entry = { value: Marshal.dump('foo'), ttl: 5000, written_at: Time.local(2021, 1, 1, 12, 0, 0) }
22
24
  subject.store[:key] = entry
23
25
 
24
- result = subject.add('key', 'value', 200)
25
- expect(result).to eq(false)
26
+ Timecop.freeze(Time.local(2021, 1, 1, 12, 0, 1)) do
27
+ result = subject.add('key', 'value', 5000)
28
+ expect(result).to eq(false)
29
+ end
26
30
 
27
31
  # stored values should not have changed
28
32
  expect(subject.store).to have_key(:key)
29
33
  expect(Marshal.load(subject.store[:key][:value])).to eq('foo')
30
- expect(subject.store[:key][:ttl]).to eq(100)
34
+ end
35
+
36
+ it 'does write the key if it exists and expiration time IS up' do
37
+ entry = { value: Marshal.dump('foo'), ttl: 50, written_at: Time.local(2021, 1, 1, 12, 0, 0) }
38
+ subject.store[:key] = entry
39
+
40
+ Timecop.freeze(Time.local(2021, 1, 1, 12, 30, 0)) do
41
+ result = subject.add('key', 'value', 50)
42
+ expect(result).to eq(true)
43
+ end
44
+
45
+ # stored values should not have changed
46
+ expect(subject.store).to have_key(:key)
47
+ expect(Marshal.load(subject.store[:key][:value])).to eq('value')
31
48
  end
32
49
  end
33
50
 
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
  require_relative 'memory_spec'
5
5
 
6
- describe 'InstanceMemory' do
6
+ describe 'SharedMemory' do
7
7
  subject { AtomicCache::Storage::SharedMemory.new }
8
8
  it_behaves_like 'memory storage'
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atomic_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.rc1
4
+ version: 0.4.1.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ibotta Developers