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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7362eab306c19b619c6eb4bd30501b2b18194cd9e870a344e45b39c6ff18b4ae
|
4
|
+
data.tar.gz: cce7666dec7a66bca3717cb7278bcdb9d7b81aaffc8c68fb020e15e02820f921
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/atomic_cache/version.rb
CHANGED
@@ -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
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
|
+
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-
|
12
|
+
date: 2021-07-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|