blunt-cache 0.1.2 → 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
  SHA1:
3
- metadata.gz: 36219a4ebbe5e011cfccc581cee7d60585f2cf96
4
- data.tar.gz: b86c47768e3bc83ec119cc432aa08130998dccf5
3
+ metadata.gz: 05f69782e6693f701b6163492ba7691080be6bdf
4
+ data.tar.gz: a543120a64bc44e0f1de54bf0d4bd5e2e9357e72
5
5
  SHA512:
6
- metadata.gz: 74d110765ae9801c3d12368a40d9bceb9e7eae4e0bbc23b61412c22d03776d7650a092e12f431551142c65854bbb77d1047016b2febf90e95c0e122b2fcabcf1
7
- data.tar.gz: 73025278992ede01a738c9b3185bd8615461c83077abee865149f97fd60718d4462c348b36207cefbf6ade24ef653d85bb114f762a89059e384a2538bcddb030
6
+ metadata.gz: 14490f56139a5d252ae1ef9e8617b69ff2b234dc80255d08a959c3568350b9cace55251fdf5ed96a449d0a4cf202f8746dad1dabad6ff6c2d255ffd513b2df67
7
+ data.tar.gz: 137f43be21cf0513e56c318d82cce66912842cea0215c157cd4b3d417606294b7d7954ebc4ff059fee7f066449954546310c292b329cf1c65f58cfc34de1d026
data/README.md CHANGED
@@ -1,31 +1,45 @@
1
1
  # BluntCache
2
2
  Simple in-memory cache service for Ruby.
3
3
 
4
+ [![Build Status](https://travis-ci.org/appelsin/blunt_cache.svg?branch=master)](https://travis-ci.org/appelsin/blunt_cache)
5
+ [![Code Climate](https://codeclimate.com/github/appelsin/blunt_cache/badges/gpa.svg)](https://codeclimate.com/github/appelsin/blunt_cache)
6
+
4
7
  ## Usage
5
8
 
6
- # set/get by key
7
- BluntCache.set "key", data
8
- data = BluntCache.get "key"
9
-
10
- # executes block if not set or expired
11
- data = BluntCache.fetch "key" do
12
- do_something
13
- end
14
-
15
- # time to live can be provided (dafault is 60 sec)
16
- BluntCache.set "key", expire: 120 data
17
- BluntCache.fetch "key", expire: 120 do
18
- do_something
19
- end
20
-
21
- # inherit it for namespacing and extending
22
- class MyCache < BluntCache
23
- @expire_default = 30
24
- end
25
-
26
- MyCache.set "1", "2"
27
- BluntCache.set "1", "3"
28
- MyCache.get "1" #2
9
+ ```ruby
10
+ # set/get by key
11
+ BluntCache.set "key", data
12
+ data = BluntCache.get "key"
13
+
14
+ # executes block if not set or expired
15
+ data = BluntCache.fetch "key" do
16
+ do_something
17
+ end
18
+
19
+ # time to live can be provided (default is 60 sec)
20
+ BluntCache.set "key", expire: 120 data
21
+ BluntCache.fetch "key", expire: 120 do
22
+ do_something
23
+ end
24
+
25
+ # checks if key exists
26
+ BluntCache.key? "nil_key" # false
27
+ BluntCache.set "nil_key", nil
28
+ BluntCache.key? "nil_key" # true
29
+
30
+ # doesn't re-executes nil fetches
31
+ data = BluntCache.fetch("heavy_key") { sleep 1; nil } # some heavy block returning nil
32
+ data = BluntCache.fetch("heavy_key") { sleep 1; nil } # doesn't re-executes if not expired
33
+
34
+ # inherit it for namespacing and extending
35
+ class MyCache < BluntCache
36
+ @expire_default = 30
37
+ end
38
+
39
+ MyCache.set "1", "2"
40
+ BluntCache.set "1", "3"
41
+ MyCache.get "1" #2
42
+ ```
29
43
 
30
44
  ## Why? When to use?
31
45
 
data/lib/blunt-cache.rb CHANGED
@@ -4,7 +4,7 @@ class BluntCache
4
4
 
5
5
  # Store +data+ in cache by +key+ for +:expire+ seconds (default is 60 sec)
6
6
  # def self.set(key, data, expire: nil)
7
- def self.set(key, data, options = {}, *args)
7
+ def self.set(key, data, options = {})
8
8
  expire = options[:expire]
9
9
  self.timestamp[key] = Time.now + (expire || self.expire_default)
10
10
  self.data[key] = data
@@ -16,14 +16,21 @@ class BluntCache
16
16
  self.timestamp[key].is_a?(Time) && Time.now < self.timestamp[key] ? self.data[key] : nil
17
17
  end
18
18
 
19
+ # Checks if key present in store.
20
+ # Returns true if key exists (even if value is false or nil) and false if key doesn't exist or expired.
21
+ def self.key?(key)
22
+ self.data.key?(key) && self.timestamp[key].is_a?(Time) && Time.now < self.timestamp[key]
23
+ end
24
+
19
25
  # Get +key+ from cache. Executes +block+, stores it's result and returns it if not set or expired.
20
26
  # def self.fetch(key, expire: nil, &block)
21
27
  def self.fetch(key, options = {}, &block)
22
28
  expire = options[:expire]
23
- result = self.get key
24
- if result.nil?
25
- self.set key, block.call, :expire => expire
29
+ if self.key?(key)
30
+ self.data[key]
26
31
  else
32
+ result = block.call
33
+ self.set key, result, :expire => expire
27
34
  result
28
35
  end
29
36
  end
@@ -34,8 +41,6 @@ class BluntCache
34
41
  @timestamp = {}
35
42
  end
36
43
 
37
- protected
38
-
39
44
  def self.data
40
45
  @data||= {}
41
46
  end
@@ -1,3 +1,3 @@
1
1
  class BluntCache
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -11,54 +11,123 @@ describe BluntCache do
11
11
 
12
12
  [BluntCache, MyCache, ShortCache].each do |c|
13
13
  context '#{c.name}' do
14
- it 'stores data' do
15
- expect { c.set("1", "1_val") }.not_to raise_error
16
- result = nil
17
- expect { result = c.get("1") }.not_to raise_error
18
- expect(result).to eq "1_val"
19
- expect { c.set("1", "1_val_2") }.not_to raise_error
20
- expect { result = c.get("1") }.not_to raise_error
21
- expect(result).to eq "1_val_2"
14
+ context 'set' do
15
+ it { expect(c.set("1", "1_val")).to eq "1_val" }
22
16
  end
23
17
 
24
- it 'returns nil on no data' do
25
- result = "value"
26
- expect { result = c.get("2") }.not_to raise_error
27
- expect(result).to eq nil
18
+ context 'get' do
19
+ before(:all) { c.set("1", "1_val") }
20
+
21
+ it { expect(c.get("1")).to eq "1_val" }
22
+
23
+ it 'returns previous set value' do
24
+ c.set("1", "1_val_2")
25
+ expect(result = c.get("1")).to eq "1_val_2"
26
+ end
27
+
28
+ it 'returns nil if not set' do
29
+ expect(c.get("2")).to eq nil
30
+ end
28
31
  end
29
32
 
30
- it 'returns value before expiration and nil after expiration' do
31
- expect { c.set("3", "3_val", :expire => 0.1) }.not_to raise_error
32
- result = nil
33
- expect { result = c.get("3") }.not_to raise_error
34
- expect(result).to eq "3_val"
35
- sleep(0.09)
36
- expect { result = c.get("3") }.not_to raise_error
37
- expect(result).to eq "3_val"
38
- sleep(0.02)
39
- expect { result = c.get("3") }.not_to raise_error
40
- expect(result).to eq nil
33
+ context 'get with expiration' do
34
+ before(:each) { c.set("3", "3_val", :expire => 0.1) }
35
+
36
+ it { expect(c.get("3")).to eq "3_val" }
37
+
38
+ it 'returns value after short sleep' do
39
+ sleep(0.09)
40
+ expect(c.get("3")).to eq "3_val"
41
+ end
42
+
43
+ it 'returns nil after long sleep' do
44
+ sleep(0.11)
45
+ expect(c.get("3")).to eq nil
46
+ end
41
47
  end
42
48
 
43
- it 'evals block in fetch if cache not set' do
44
- expect(c.get("4")).to eq nil
45
- result = nil
46
- expect { result = c.fetch("4") do "4_val" end }.not_to raise_error
47
- expect(result).to eq "4_val"
48
- expect(c.get("4")).to eq "4_val"
49
- expect { result = c.fetch("4") do "4_val_2" end }.not_to raise_error
50
- expect(result).to eq "4_val"
49
+ context 'key?' do
50
+ before(:all) { c.set("k1", "k1_val") }
51
+
52
+ it { expect(c.key?("k1")).to eq true }
53
+ it 'returns false if not set' do
54
+ expect(c.key?("k2")).to eq false
55
+ end
56
+ end
57
+
58
+ context 'key? with expiration' do
59
+ before(:each) { c.set("k3", "k3_val", :expire => 0.1) }
60
+
61
+ it { expect(c.key?("k3")).to eq true }
62
+
63
+ it 'returns true after short sleep' do
64
+ sleep(0.09)
65
+ expect(c.key?("k3")).to eq true
66
+ end
67
+
68
+ it 'returns false after long sleep' do
69
+ sleep(0.11)
70
+ expect(c.key?("k3")).to eq false
71
+ end
72
+ end
73
+
74
+ context 'fetch' do
75
+ it 'executes block for a first time' do
76
+ executed = :not_executed
77
+ expect(c.fetch("4-0") { executed = :executed }).to eq :executed
78
+ expect(executed).to eq :executed
79
+ end
80
+
81
+ it 'sets value from block' do
82
+ expect(c.get("4")).to eq nil #pre-check
83
+ expect(c.fetch("4") { "4_val" }).to eq "4_val"
84
+ end
85
+
86
+ it 'doesn\'t execute block for a second time' do
87
+ expect(c.fetch("4-1") { :executed_first }).to eq :executed_first
88
+ executed = :not_executed
89
+ expect(c.fetch("4-1") { executed = :executed_second }).to eq :executed_first
90
+ expect(executed).to eq :not_executed
91
+ end
92
+ end
93
+
94
+ context 'fetch with experation' do
95
+ it 'recieves :expire' do
96
+ expect( c.fetch("6", :expire => 0.1) { "6_val" } ).to eq "6_val"
97
+ end
98
+
99
+ it 'doesn\'t execute block for a second time' do
100
+ expect(c.fetch("6-1", :expire => 0.1) { :executed_first }).to eq :executed_first
101
+ executed = :not_executed
102
+ expect(c.fetch("6-1", :expire => 0.1) { executed = :executed_second }).to eq :executed_first
103
+ expect(executed).to eq :not_executed
104
+ end
105
+
106
+ it 'doesn\'t execute block for a second time after short sleep' do
107
+ expect(c.fetch("6-2", :expire => 0.1) { :executed_first }).to eq :executed_first
108
+ executed = :not_executed
109
+ sleep(0.09)
110
+ expect(c.fetch("6-2", :expire => 0.1) { executed = :executed_second }).to eq :executed_first
111
+ expect(executed).to eq :not_executed
112
+ end
113
+
114
+ it 'executes block for a second time after long sleep' do
115
+ expect(c.fetch("6-3", :expire => 0.1) { :executed_first }).to eq :executed_first
116
+ executed = :not_executed
117
+ sleep(0.11)
118
+ expect(c.fetch("6-3", :expire => 0.1) { executed = :executed_second }).to eq :executed_second
119
+ expect(executed).to eq :executed_second
120
+ end
51
121
  end
52
122
 
53
- it 'returns value before expiration and re-executes block after expiration (fetch)' do
54
- result = nil
55
- expect { result = c.fetch "6", :expire => 0.1 do "6_val" end }.not_to raise_error
56
- expect(result).to eq "6_val"
57
- expect { result = c.fetch "6" do "6_val_2" end }.not_to raise_error
58
- expect(result).to eq "6_val"
59
- sleep(0.11)
60
- expect { result = c.fetch "6" do "6_val_3" end }.not_to raise_error
61
- expect(result).to eq "6_val_3"
123
+ context 'fetch with nil' do
124
+ it 'doesn\'t re-executes for nil value' do
125
+ execution_counter = 0
126
+ expect(c.fetch("nill") { execution_counter+= 1; nil }).to eq nil
127
+ expect(execution_counter).to eq 1
128
+ expect(c.fetch("nill") { execution_counter+= 1; nil }).to eq nil
129
+ expect(execution_counter).to eq 1
130
+ end
62
131
  end
63
132
  end
64
133
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blunt-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Exempliarov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,10 +90,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.4.8
93
+ rubygems_version: 2.6.4
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Simple in-memory cache service for Ruby.
97
97
  test_files:
98
98
  - spec/blunt_cache_spec.rb
99
99
  - spec/spec_helper.rb
100
+ has_rdoc: