zache 0.1.0 → 0.2.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: 54dd40fa91ad83f55060c983e59e4e371064313a10954e10a616fb991b8d1f43
4
- data.tar.gz: 3cbf51535b7b696e8d02fbb917739b3a46d9a39e22416e3b4de0b0527fc2eb75
3
+ metadata.gz: 186b5dcd33ca13d027053e477dc378e8cd203d96e43257f591d8537d8b665c17
4
+ data.tar.gz: c39d96fefe90f9b481bbd6acd49ac4d2cd7cd675d83462b53895a8fc2ef1f49a
5
5
  SHA512:
6
- metadata.gz: 1afff4f4fcf1f5250e291486a324a97bb8f78ce70eb042293c7f9d06bc745b043e1329c725e089f9b7579e389c405bad7469d085961dc3e909a00c0b46813d85
7
- data.tar.gz: 2e88abe56d9447451253aa3a0829451ece1c8a6393cb2dd9cb1676bc0cc062722f5bd2bdf446a25cae8cd78cca3576a78f54553a408b5b6c5e67a0459559ce53
6
+ metadata.gz: fd75a39fa850377dc87c9f6658e6bd03f49304a7b167ff6b6e59ba676ca6a6b727333a1cc83248fcd9b45dd3ad118911e092de60991d4fd4ed8c35a700a80dcc
7
+ data.tar.gz: 110ad00c04c2d65c65bf9c875dd21c9b9e3254412eae28766cf53be3cad07ebe9e81a09de881f0aec1d2747fcf35ca0f461503bcc2a5c47dacd1f35dc29c47f3
data/README.md CHANGED
@@ -21,6 +21,14 @@ zache = Zache.new
21
21
  # Expires in 5 minutes
22
22
  v = zache.get(:count, lifetime: 5 * 60) { expensive() }
23
23
  ```
24
+
25
+ By default `Zache` is thread-safe. You turn that off by using `sync` argument:
26
+
27
+ ```ruby
28
+ zache = Zache.new(sync: false)
29
+ v = zache.get(:count) { expensive() }
30
+ ```
31
+
24
32
  That's it.
25
33
 
26
34
  # How to contribute
data/lib/zache.rb CHANGED
@@ -27,23 +27,34 @@
27
27
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
28
28
  # License:: MIT
29
29
  class Zache
30
- def initialize
30
+ def initialize(sync: true)
31
31
  @hash = {}
32
+ @sync = sync
32
33
  @mutex = Mutex.new
33
34
  end
34
35
 
35
36
  def get(key, lifetime: 60 * 60)
36
- @mutex.synchronize do
37
- rec = @hash[key]
38
- rec = nil if !rec.nil? && rec[:start] < Time.now - rec[:lifetime]
39
- if rec.nil?
40
- @hash[key] = {
41
- value: yield,
42
- start: Time.now,
43
- lifetime: lifetime
44
- }
37
+ if @sync
38
+ @mutex.synchronize do
39
+ calc(key, lifetime) { yield }
45
40
  end
46
- @hash[key][:value]
41
+ else
42
+ calc(key, lifetime) { yield }
47
43
  end
48
44
  end
45
+
46
+ private
47
+
48
+ def calc(key, lifetime)
49
+ rec = @hash[key]
50
+ rec = nil if !rec.nil? && rec[:start] < Time.now - rec[:lifetime]
51
+ if rec.nil?
52
+ @hash[key] = {
53
+ value: yield,
54
+ start: Time.now,
55
+ lifetime: lifetime
56
+ }
57
+ end
58
+ @hash[key][:value]
59
+ end
49
60
  end
data/test/test_zache.rb CHANGED
@@ -32,7 +32,7 @@ require_relative '../lib/zache'
32
32
  # License:: MIT
33
33
  class ZacheTest < Minitest::Test
34
34
  def test_caches
35
- cache = Zache.new
35
+ cache = Zache.new(sync: false)
36
36
  first = cache.get(:hey, lifetime: 5) { Random.rand }
37
37
  second = cache.get(:hey) { Random.rand }
38
38
  assert(first == second)
data/zache.gemspec CHANGED
@@ -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.1.0'
34
+ s.version = '0.2.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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko