atomic_cache 0.4.1.rc1 → 0.5.0.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: db2b157ed872e6cb90af8cf2ef61a97f237485bc763998464eb4faf488d26c11
4
- data.tar.gz: 48e13e212479454f2ece192c81c8f755a4aa039a2cf5930cf1cdfea2eaae438d
3
+ metadata.gz: 7362eab306c19b619c6eb4bd30501b2b18194cd9e870a344e45b39c6ff18b4ae
4
+ data.tar.gz: cce7666dec7a66bca3717cb7278bcdb9d7b81aaffc8c68fb020e15e02820f921
5
5
  SHA512:
6
- metadata.gz: 5fb9aba2eaeb20e9e3206c80039941b005c7f6e82c05bfddd59482783b028706f706a6843a8ea8261d18895732035d1deeabda178e5437620f33ca9abf88b54e
7
- data.tar.gz: 06511e8a0ec9b33de005994f6938877f85b053c03072f52b6cc0dbd22a99389dfb36245f7002d59ea364d1ea583eec0b440c6bd2a5e1b84604434474275d6fe2
6
+ metadata.gz: b418f307870a20aa99b326644c14066d3baba30287138ce2a4b533582d788231058c0f3f66737fcf19225cdadbcf5dbb78653d7969ade7eb7555a7c3f849b4c2
7
+ data.tar.gz: c9f48abddc3b0f9d623db83bd493df4fd555ae468710251dd13db6ed684399052b52a48ffec516f96ae17a1ccc747416688b2234f514ef8278bba0fd4ed550d2
data/docs/USAGE.md CHANGED
@@ -72,6 +72,13 @@ All incoming keys are normalized to symbols. All values are stored with a `valu
72
72
 
73
73
  It's likely preferable to use an environments file to configure the `key_storage` and `cache_storage` to always be an in-memory adapter when running in the test environment instead of manually configuring the storage adapter per spec.
74
74
 
75
+ #### TTL in Tests
76
+ In a test environment, unlike in a production environment, database queries are fast, and time doesn't elapse quite like it does in the real world. As tests get more complex, they perform changes for which they expect the cache to expire. However, because of the synthetic nature of testing, TTLs, particularly those on locks, don't quite work the same either.
77
+
78
+ There are a few approaches to address this, for example, using `sleep` to cause real time to pass (not preferable) or wrapping each test in a TimeCop, forcing time to pass (works but quite manual).
79
+
80
+ Since this situation is highly likely to arise, `atomic_cache` provides a feature to globally disable enforcing TTL on locks for the `SharedMemory` implementation. Set `enforce_ttl = false` to disable TTL checking on locks within SharedMemory in a test context. This will prevent tests from failing due to unexpired TTLs on locks.
81
+
75
82
  #### ★ Testing Tip ★
76
83
  If using `SharedMemory` for integration style tests, a global `before(:each)` can be configured in `spec_helper.rb`.
77
84
 
@@ -79,9 +86,10 @@ If using `SharedMemory` for integration style tests, a global `before(:each)` ca
79
86
  # spec/spec_helper.rb
80
87
  RSpec.configure do |config|
81
88
 
82
- #your other config
89
+ # your other config
83
90
 
84
91
  config.before(:each) do
92
+ AtomicCache::Storage::SharedMemory.enforce_ttl = false
85
93
  AtomicCache::Storage::SharedMemory.reset
86
94
  end
87
95
  end
@@ -10,6 +10,21 @@ module AtomicCache
10
10
  STORE = {}
11
11
  SEMAPHORE = Mutex.new
12
12
 
13
+ @enforce_ttl = true
14
+ class << self
15
+ attr_accessor :enforce_ttl
16
+ end
17
+
18
+ def add(raw_key, new_value, ttl, user_options={})
19
+ if self.class.enforce_ttl
20
+ super(raw_key, new_value, ttl, user_options)
21
+ else
22
+ store_op(raw_key, user_options) do |key, options|
23
+ write(key, new_value, ttl, user_options)
24
+ end
25
+ end
26
+ end
27
+
13
28
  def self.reset
14
29
  STORE.clear
15
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AtomicCache
4
- VERSION = "0.4.1.rc1"
4
+ VERSION = "0.5.0.rc1"
5
5
  end
@@ -17,8 +17,6 @@ shared_examples 'memory storage' do
17
17
  expect(result).to eq(true)
18
18
  end
19
19
 
20
- # SharedMemory.new.add("foo", ttl: 100)
21
-
22
20
  it 'does not write the key if it exists but expiration time is NOT up' do
23
21
  entry = { value: Marshal.dump('foo'), ttl: 5000, written_at: Time.local(2021, 1, 1, 12, 0, 0) }
24
22
  subject.store[:key] = entry
@@ -6,4 +6,18 @@ require_relative 'memory_spec'
6
6
  describe 'SharedMemory' do
7
7
  subject { AtomicCache::Storage::SharedMemory.new }
8
8
  it_behaves_like 'memory storage'
9
+
10
+ context 'enforce_ttl disabled' do
11
+ before(:each) do
12
+ AtomicCache::Storage::SharedMemory.enforce_ttl = false
13
+ end
14
+
15
+ it 'allows instantly `add`ing keys' do
16
+ subject.add("foo", 1, ttl: 100000)
17
+ subject.add("foo", 2, ttl: 1)
18
+
19
+ expect(subject.store).to have_key(:foo)
20
+ expect(Marshal.load(subject.store[:foo][:value])).to eq(2)
21
+ end
22
+ end
9
23
  end
data/spec/spec_helper.rb CHANGED
@@ -17,4 +17,8 @@ RSpec.configure do |config|
17
17
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
18
18
  expectations.syntax = :expect
19
19
  end
20
+
21
+ config.before(:each) do
22
+ AtomicCache::Storage::SharedMemory.enforce_ttl = true
23
+ end
20
24
  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.1.rc1
4
+ version: 0.5.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ibotta Developers
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-07-07 00:00:00.000000000 Z
12
+ date: 2021-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler