kt 0.2.0 → 0.3.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 +4 -4
- data/lib/kt.rb +14 -0
- data/lib/kt/version.rb +1 -1
- data/spec/kt_spec.rb +73 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/spec_support.rb +4 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c88c06423582454afa5b7c60bd08c21d344cdbb
|
4
|
+
data.tar.gz: 45903ecb734768ec7a7cb7a99c38a4d205b5fd52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cc9fc06eabb1245f8be0caf08ef53b2ade72f17788b14fce0216e394a78393a7c870c9690564f90028dd6ff902c85a8a0056e6b37f04ff92eb77b69c4d8647e
|
7
|
+
data.tar.gz: b8e49d987694eae0fbcc8ce9b31b31a48264322568cfa5d320cdf5b8674dc57aa18fa1a96facf8525e5a359b0d2849544402c3619a6f5fcaaa2370e713b90435
|
data/lib/kt.rb
CHANGED
@@ -102,6 +102,20 @@ class KT
|
|
102
102
|
return res
|
103
103
|
end
|
104
104
|
|
105
|
+
# fetch retrives the keys from cache
|
106
|
+
# if key is found it returns the unmarshaled value
|
107
|
+
# if key is not found it runs the block sends the value and returns it
|
108
|
+
def fetch(key, &block)
|
109
|
+
value = get(key)
|
110
|
+
if value
|
111
|
+
Marshal::load(value)
|
112
|
+
else
|
113
|
+
block.call.tap do |value|
|
114
|
+
set(key, Marshal::dump(value))
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
105
119
|
# ttl returns the time to live for a key in seconds
|
106
120
|
# if key does not exist, it returns -2
|
107
121
|
# if key exists but no ttl is set, it returns -1
|
data/lib/kt/version.rb
CHANGED
data/spec/kt_spec.rb
CHANGED
@@ -93,6 +93,79 @@ describe KT do
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
|
+
describe "fetch" do
|
97
|
+
it "fetches block if not found in cache" do
|
98
|
+
block_ran = false
|
99
|
+
user = @kt.fetch("my.key") do
|
100
|
+
block_ran = true
|
101
|
+
User.new(
|
102
|
+
:id => 1,
|
103
|
+
:name => "Aaa bbb",
|
104
|
+
:email => "foo@example.com",
|
105
|
+
:age => 20
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
109
|
+
expect(block_ran).to eql(true)
|
110
|
+
expect(user.id).to eql(1)
|
111
|
+
expect(user.name).to eql("Aaa bbb")
|
112
|
+
expect(user.email).to eql("foo@example.com")
|
113
|
+
expect(user.age).to eql(20)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "fetches from cache if already in cache" do
|
117
|
+
block_ran = false
|
118
|
+
user1 = @kt.fetch("my.key2") do
|
119
|
+
block_ran = true
|
120
|
+
User.new(
|
121
|
+
:id => 1,
|
122
|
+
:name => "Aaa bbb",
|
123
|
+
:email => "foo@example.com",
|
124
|
+
:age => 20
|
125
|
+
)
|
126
|
+
end
|
127
|
+
expect(block_ran).to eql(true)
|
128
|
+
|
129
|
+
block_ran = false
|
130
|
+
|
131
|
+
user2 = @kt.fetch("my.key2") do
|
132
|
+
block_ran = true
|
133
|
+
User.new(
|
134
|
+
:id => 1,
|
135
|
+
:name => "Aaa bbb",
|
136
|
+
:email => "foo@example.com",
|
137
|
+
:age => 20
|
138
|
+
)
|
139
|
+
end
|
140
|
+
|
141
|
+
expect(block_ran).to eql(false)
|
142
|
+
expect(user1).to eql(user2)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "sets cache if not found in cache" do
|
146
|
+
block_ran = false
|
147
|
+
user1 = @kt.fetch("my.key3") do
|
148
|
+
block_ran = true
|
149
|
+
User.new(
|
150
|
+
:id => 2,
|
151
|
+
:name => "Aaa ccc",
|
152
|
+
:email => "foo2@example.com",
|
153
|
+
:age => 30
|
154
|
+
)
|
155
|
+
end
|
156
|
+
expect(block_ran).to eql(true)
|
157
|
+
|
158
|
+
obj = @kt.get("my.key3")
|
159
|
+
user = Marshal::load(obj)
|
160
|
+
expect(user.id).to eql(2)
|
161
|
+
expect(user.name).to eql("Aaa ccc")
|
162
|
+
expect(user.email).to eql("foo2@example.com")
|
163
|
+
expect(user.age).to eql(30)
|
164
|
+
|
165
|
+
expect(user).to eql(user1)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
96
169
|
describe "bulk" do
|
97
170
|
it "returns nil hash for not found keys" do
|
98
171
|
expect(@kt.get_bulk(["foo1", "foo2", "foo3"])).to eql({})
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Teodor Pripoae
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: excon
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/kt/version.rb
|
116
116
|
- spec/kt_spec.rb
|
117
117
|
- spec/spec_helper.rb
|
118
|
+
- spec/spec_support.rb
|
118
119
|
homepage: https://github.com/kuende/kt-ruby
|
119
120
|
licenses:
|
120
121
|
- Apache-2.0
|
@@ -142,3 +143,4 @@ summary: Kyoto Tycoon client for ruby. For more information see the Readme on gi
|
|
142
143
|
test_files:
|
143
144
|
- spec/kt_spec.rb
|
144
145
|
- spec/spec_helper.rb
|
146
|
+
- spec/spec_support.rb
|