zache 0.3.1 → 0.4.0

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: d27ca46de41af5519d5ad841403ce28c1527e8c7a2ec2243cb32c4c4924a684c
4
- data.tar.gz: 0eab718467f7ee55909fc3d0aa7c4b6ac11881f38c7d6f2deaaa01cc231a259e
3
+ metadata.gz: 91965defa678c5b684064d7b868b46f7f8b31c9192209d6eb2f659055e790056
4
+ data.tar.gz: 35d397d7fa7179e0968b564cad219a8e1aedaf0c2efc92e4fb7079faaec36d49
5
5
  SHA512:
6
- metadata.gz: d05683cac3f782074b17f4fb0a0a26e025f6f5ed4c887c338f1f940079cc122601c02f81ed035789ca894ed93aa046dda3522f6f96a712c7f5c800192d6edcb4
7
- data.tar.gz: b3b691d80b84f24c5542c0e33932fa541624aefa0f8ee216aef444788cafba7d7dbabf05694e83c3f340b64a18dd67048bad25edcd4c557c1fcdb7119630f724
6
+ metadata.gz: 88c5c68c7438dd4a34abc24a529815abb2020a4fc2502bab0d374fcc75e79b5bfb447c1f4b82d72cc7787c5bd0c0cb2aae8176da9a332b33bb8880ef3c286c4d
7
+ data.tar.gz: 734f9721d3e726ba433881a325a724e4e2cfa45c5b94962753879d61dc33d9ed763ec1bb144b2042c87673746cfd753a7b7b708b7c83ff610777caa1b5ed8ffa
@@ -5,6 +5,8 @@ AllCops:
5
5
  Metrics/ClassLength:
6
6
  Exclude:
7
7
  - 'test/*.rb'
8
+ Layout/EmptyLineAfterGuardClause:
9
+ Enabled: false
8
10
  Layout/MultilineMethodCallIndentation:
9
11
  Enabled: false
10
12
  Metrics/AbcSize:
@@ -51,10 +51,21 @@ class Zache
51
51
 
52
52
  # Gets the value from the cache by the provided key. If the value is not
53
53
  # found in the cache, it will be calculated via the provided block. If
54
- # the block is not given, an exception will be raised.
55
- def get(key, lifetime: 60 * 60)
56
- raise 'A block is required' unless block_given?
57
- synchronized { calc(key, lifetime) { yield } }
54
+ # the block is not given, an exception will be raised. The lifetime
55
+ # must be in seconds. The default lifetime is huge, which means that the
56
+ # key will never be expired.
57
+ def get(key, lifetime: 2**32)
58
+ if block_given?
59
+ synchronized { calc(key, lifetime) { yield } }
60
+ else
61
+ rec = @hash[key]
62
+ if key_expired?(key)
63
+ @hash.delete(key)
64
+ rec = nil
65
+ end
66
+ raise 'The key is absent in the cache' if rec.nil?
67
+ rec[:value]
68
+ end
58
69
  end
59
70
 
60
71
  # Checks whether the value exists in the cache by the provided key. Returns
@@ -69,16 +80,29 @@ class Zache
69
80
  !rec.nil?
70
81
  end
71
82
 
83
+ # Put a value into the cache.
84
+ def put(key, value, lifetime: 2**32)
85
+ synchronized do
86
+ @hash[key] = {
87
+ value: value,
88
+ start: Time.now,
89
+ lifetime: lifetime
90
+ }
91
+ end
92
+ end
93
+
72
94
  # Removes the value from the hash, by the provied key. If the key is absent
73
95
  # and the block is provide, the block will be called.
74
96
  def remove(key)
75
97
  synchronized { @hash.delete(key) { yield if block_given? } }
76
98
  end
77
99
 
100
+ # Remove all keys from the cache.
78
101
  def remove_all
79
102
  synchronized { @hash = {} }
80
103
  end
81
104
 
105
+ # Remove keys that are expired.
82
106
  def clean
83
107
  synchronized { @hash.delete_if { |_key, value| key_expired?(value) } }
84
108
  end
@@ -157,8 +157,15 @@ class ZacheTest < Minitest::Test
157
157
  assert(cache.exists?(:bye) == false)
158
158
  end
159
159
 
160
+ def test_puts_something_in
161
+ cache = Zache.new(sync: false)
162
+ cache.get(:hey) { Random.rand }
163
+ cache.put(:hey, 123)
164
+ assert_equal(123, cache.get(:hey))
165
+ end
166
+
160
167
  def test_sync_zache_is_reentrant
161
168
  cache = Zache.new
162
- cache.get(:first) { cache.get(:second) { 1 } }
169
+ cache.get(:first) { cache.get(:second) { cache.get(:third) { 1 } } }
163
170
  end
164
171
  end
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.rubygems_version = '2.3.3'
32
32
  s.required_ruby_version = '>=2.3'
33
33
  s.name = 'zache'
34
- s.version = '0.3.1'
34
+ s.version = '0.4.0'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'In-memory Cache'
37
37
  s.description = 'Zero-footprint in-memory thread-safe cache'
@@ -42,10 +42,10 @@ Gem::Specification.new do |s|
42
42
  s.test_files = s.files.grep(%r{^(test)/})
43
43
  s.rdoc_options = ['--charset=UTF-8']
44
44
  s.extra_rdoc_files = ['README.md']
45
- s.add_development_dependency 'minitest', '~>5'
46
- s.add_development_dependency 'rake', '~>12'
47
- s.add_development_dependency 'rdoc', '~>4'
48
- s.add_development_dependency 'rubocop', '0.58.1'
49
- s.add_development_dependency 'rubocop-rspec', '~>1'
50
- s.add_development_dependency 'threads', '~>0'
45
+ s.add_development_dependency 'minitest', '5.11.3'
46
+ s.add_development_dependency 'rake', '12.3.1'
47
+ s.add_development_dependency 'rdoc', '4.3.0'
48
+ s.add_development_dependency 'rubocop', '0.60.0'
49
+ s.add_development_dependency 'rubocop-rspec', '1.30.1'
50
+ s.add_development_dependency 'threads', '0.3.0'
51
51
  end
metadata CHANGED
@@ -1,99 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.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: 2018-11-11 00:00:00.000000000 Z
11
+ date: 2018-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
19
+ version: 5.11.3
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '5'
26
+ version: 5.11.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: '12'
33
+ version: 12.3.1
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: '12'
40
+ version: 12.3.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: '4'
47
+ version: 4.3.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: '4'
54
+ version: 4.3.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.58.1
61
+ version: 0.60.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 0.58.1
68
+ version: 0.60.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop-rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: '1'
75
+ version: 1.30.1
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: '1'
82
+ version: 1.30.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: threads
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: 0.3.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 0.3.0
97
97
  description: Zero-footprint in-memory thread-safe cache
98
98
  email: yegor256@gmail.com
99
99
  executables: []