lock_and_cache 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05cf8fb6492276dbdd927c7b40877a3115dd4798
4
- data.tar.gz: 9d522afcc3bbed01ce5876f0465f2cbe655b96bb
3
+ metadata.gz: 58a808fd3a27573b09b72e166416c7c83f18c87c
4
+ data.tar.gz: 935f944078a81f0801fe6efa9ab62f19e931ae75
5
5
  SHA512:
6
- metadata.gz: 47ff5574928c53bc0750202ea7808a6565f01e5089928469b4f99aff596ced3f63356100ec85b7d32cdbb6a0872ab90819d28e382b9587d0df118fb8271c5a1b
7
- data.tar.gz: dc3be7f40572214c213a6928e24cfb84c2c0734b2c3af4238eb569bc625f8aabc0d642cc115ae772c355e190ad12adb0508e69ab85999074d3c42eb9b5bceb8a
6
+ metadata.gz: b055a2757a283e0ec4eaf5529cc9eb86a5dbfad18e1ec335a0fbb475066b502d555cad457b5a596c41cb1179727b97675fb5a8fa1b5676d8632c728c7de06877
7
+ data.tar.gz: ed3e65b4378637c92749d2dbf4bfa835026d52425e923aab2db898d1cb86c69487e3233334e254a6cc02125b0930e3a6dffdf8610133bccb1b3203c59104465f
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.1.2 / 2015-06-24
2
+
3
+ * Enhancements
4
+
5
+ * Add :expires option in seconds
6
+
1
7
  0.1.1 / 2015-02-04
2
8
 
3
9
  * Enhancements
@@ -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
- storage.set digest, ::Marshal.dump(memo)
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
@@ -1,3 +1,3 @@
1
1
  module LockAndCache
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -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.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-02-04 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord