zache 0.10.1 → 0.11.0

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
  SHA256:
3
- metadata.gz: aff25777e250a00476b61b3b8ff9e9d8492546b9e4c53fbdba28f7cba3c64a95
4
- data.tar.gz: d7f7214d2829b7e2d513fa36b0de36326d9453ad33a41f21ab64485a263c3b6c
3
+ metadata.gz: 4686166302287dae583252d66cd7d1a265a9fea6f5cc6c68f321249a9fe04d5e
4
+ data.tar.gz: 7d960c4c23fa027e623f033a3ba618802d43d6d65a8b863c1ce99eb297fdacff
5
5
  SHA512:
6
- metadata.gz: 401d5bcfb34777d3d0e0b22b0c65802bdd273eb9b3076710cfd8a31c63fa3f8f86fdda5c965f28bc599045c42fc24c187c0fa2bccb7fbd014cdfced6232bdcc1
7
- data.tar.gz: 8993cc92f22c9941b457b8dfaa8ae86893015d996bde9ddcbc10a1459d2358310cea93097477f6f43fc7320e6e6b0f21e38a1a32fcedc1298edd0520f032dde6
6
+ metadata.gz: ef2ef6578ec02be0572e92f1e45ff104f9039bb3b189c5f2647c80d69a0c8f82a27bdd2f3238770b09c877b2424ace681b59f232713c6457cec07cf716ea85bf
7
+ data.tar.gz: f1651dd070af026762a57f2a1b34f45cceae1bac625a4b0bd5c32f1492615cf4e54f12fddaa8d50f19f84679b7d2096285670b839b79d190b50844531a865b8b
@@ -96,13 +96,13 @@ class Zache
96
96
  # calculated result will be returned if it exists and is already expired.
97
97
  def get(key, lifetime: 2**32, dirty: false)
98
98
  if block_given?
99
- if (dirty || @dirty) && locked? && key_expired?(key) && @hash.key?(key)
99
+ if (dirty || @dirty) && locked? && expired?(key) && @hash.key?(key)
100
100
  return @hash[key][:value]
101
101
  end
102
102
  synchronized { calc(key, lifetime) { yield } }
103
103
  else
104
104
  rec = @hash[key]
105
- if key_expired?(key)
105
+ if expired?(key)
106
106
  return rec[:value] if dirty || @dirty
107
107
  @hash.delete(key)
108
108
  rec = nil
@@ -117,13 +117,20 @@ class Zache
117
117
  # it will be removed by this method and the result will be FALSE.
118
118
  def exists?(key, dirty: false)
119
119
  rec = @hash[key]
120
- if key_expired?(key) && !dirty && !@dirty
120
+ if expired?(key) && !dirty && !@dirty
121
121
  @hash.delete(key)
122
122
  rec = nil
123
123
  end
124
124
  !rec.nil?
125
125
  end
126
126
 
127
+ # Checks whether the key exists in the cache and is expired. If the
128
+ # key is absent FALSE is returned.
129
+ def expired?(key)
130
+ rec = @hash[key]
131
+ !rec.nil? && rec[:start] < Time.now - rec[:lifetime]
132
+ end
133
+
127
134
  # Returns the modification time of the key, if it exists.
128
135
  # If not, current time is returned.
129
136
  def mtime(key)
@@ -160,14 +167,14 @@ class Zache
160
167
 
161
168
  # Remove keys that are expired.
162
169
  def clean
163
- synchronized { @hash.delete_if { |_key, value| key_expired?(value) } }
170
+ synchronized { @hash.delete_if { |_key, value| expired?(value) } }
164
171
  end
165
172
 
166
173
  private
167
174
 
168
175
  def calc(key, lifetime)
169
176
  rec = @hash[key]
170
- rec = nil if key_expired?(key)
177
+ rec = nil if expired?(key)
171
178
  if rec.nil?
172
179
  @hash[key] = {
173
180
  value: yield,
@@ -191,9 +198,4 @@ class Zache
191
198
  yield
192
199
  end
193
200
  end
194
-
195
- def key_expired?(key)
196
- rec = @hash[key]
197
- !rec.nil? && rec[:start] < Time.now - rec[:lifetime]
198
- end
199
201
  end
@@ -219,6 +219,7 @@ class ZacheTest < Minitest::Test
219
219
  sleep 0.1
220
220
  Timeout.timeout(1) do
221
221
  assert(cache.exists?(:x))
222
+ assert(cache.expired?(:x))
222
223
  assert_equal(1, cache.get(:x))
223
224
  assert_equal(1, cache.get(:x) { 2 })
224
225
  end
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.rubygems_version = '2.5'
32
32
  s.required_ruby_version = '>=2.5'
33
33
  s.name = 'zache'
34
- s.version = '0.10.1'
34
+ s.version = '0.11.0'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'In-memory Cache'
37
37
  s.description = 'Zero-footprint in-memory thread-safe cache'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-08 00:00:00.000000000 Z
11
+ date: 2019-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby