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 +4 -4
- data/README.md +8 -0
- data/lib/zache.rb +22 -11
- data/test/test_zache.rb +1 -1
- data/zache.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 186b5dcd33ca13d027053e477dc378e8cd203d96e43257f591d8537d8b665c17
|
4
|
+
data.tar.gz: c39d96fefe90f9b481bbd6acd49ac4d2cd7cd675d83462b53895a8fc2ef1f49a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
@
|
37
|
-
|
38
|
-
|
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
|
-
|
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.
|
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'
|