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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db2b157ed872e6cb90af8cf2ef61a97f237485bc763998464eb4faf488d26c11
|
4
|
+
data.tar.gz: 48e13e212479454f2ece192c81c8f755a4aa039a2cf5930cf1cdfea2eaae438d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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),
|
data/lib/atomic_cache/version.rb
CHANGED
@@ -17,17 +17,34 @@ shared_examples 'memory storage' do
|
|
17
17
|
expect(result).to eq(true)
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
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
|
-
|
25
|
-
|
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
|
-
|
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
|
|