lock_and_cache 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +6 -0
- data/lib/lock_and_cache.rb +7 -1
- data/lib/lock_and_cache/version.rb +1 -1
- data/spec/lock_and_cache_spec.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58a808fd3a27573b09b72e166416c7c83f18c87c
|
4
|
+
data.tar.gz: 935f944078a81f0801fe6efa9ab62f19e931ae75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b055a2757a283e0ec4eaf5529cc9eb86a5dbfad18e1ec335a0fbb475066b502d555cad457b5a596c41cb1179727b97675fb5a8fa1b5676d8632c728c7de06877
|
7
|
+
data.tar.gz: ed3e65b4378637c92749d2dbf4bfa835026d52425e923aab2db898d1cb86c69487e3233334e254a6cc02125b0930e3a6dffdf8610133bccb1b3203c59104465f
|
data/CHANGELOG
CHANGED
data/lib/lock_and_cache.rb
CHANGED
@@ -66,6 +66,8 @@ module LockAndCache
|
|
66
66
|
debug = (ENV['LOCK_AND_CACHE_DEBUG'] == 'true')
|
67
67
|
caller[0] =~ /in `(\w+)'/
|
68
68
|
method_id = $1 or raise "couldn't get method_id from #{kaller[0]}"
|
69
|
+
options = key_parts.pop.stringify_keys if key_parts.last.is_a?(Hash)
|
70
|
+
expires = options['expires'] if options
|
69
71
|
key = LockAndCache::Key.new self, method_id, key_parts
|
70
72
|
digest = key.digest
|
71
73
|
storage = LockAndCache.storage
|
@@ -81,7 +83,11 @@ module LockAndCache
|
|
81
83
|
else
|
82
84
|
Thread.exclusive { $stderr.puts "[lock_and_cache] D #{key.debug}" } if debug
|
83
85
|
memo = yield
|
84
|
-
|
86
|
+
if expires
|
87
|
+
storage.setex digest, expires, ::Marshal.dump(memo)
|
88
|
+
else
|
89
|
+
storage.set digest, ::Marshal.dump(memo)
|
90
|
+
end
|
85
91
|
memo
|
86
92
|
end
|
87
93
|
end
|
data/spec/lock_and_cache_spec.rb
CHANGED
@@ -6,6 +6,7 @@ class Foo
|
|
6
6
|
def initialize(id)
|
7
7
|
@id = id
|
8
8
|
@count = 0
|
9
|
+
@count_exp = 0
|
9
10
|
end
|
10
11
|
|
11
12
|
def click
|
@@ -14,6 +15,12 @@ class Foo
|
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
18
|
+
def click_exp
|
19
|
+
lock_and_cache(self, expires: 1, foo: :bar) do
|
20
|
+
@count_exp += 1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
17
24
|
def lock_and_cache_key
|
18
25
|
@id
|
19
26
|
end
|
@@ -75,6 +82,13 @@ describe LockAndCache do
|
|
75
82
|
foo.lock_and_cache_clear :click, foo
|
76
83
|
expect(foo.click).to eq(2)
|
77
84
|
end
|
85
|
+
|
86
|
+
it "can be expired" do
|
87
|
+
expect(foo.click_exp).to eq(1)
|
88
|
+
expect(foo.click_exp).to eq(1)
|
89
|
+
sleep 2
|
90
|
+
expect(foo.click_exp).to eq(2)
|
91
|
+
end
|
78
92
|
end
|
79
93
|
|
80
94
|
describe "locking" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lock_and_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Seamus Abshere
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|