kt 0.2.0 → 0.3.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: 022dd80235959cbdaeacfe9d986b338d9cffd078
4
- data.tar.gz: a603ea0e5b9e7735fbf576a4b1be6cc6137856a5
3
+ metadata.gz: 2c88c06423582454afa5b7c60bd08c21d344cdbb
4
+ data.tar.gz: 45903ecb734768ec7a7cb7a99c38a4d205b5fd52
5
5
  SHA512:
6
- metadata.gz: a48f0af21e611269d6c7c9d4a85b9403a770c1bbee19f659db9cc07c937282fc304caa2232097e7f26327646760f52580a38d9923faf6e47bdd0e1a9b0cbc5d0
7
- data.tar.gz: 5d82caf9dc64d7de2c1a1506b66229b9bf659944672b4a40df13bda9bbf30e5ee8f250a5ed5ed60b4c3c0a07c17ddd13f4561c37e15babdcbfea99ae90f7eef3
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
@@ -1,3 +1,3 @@
1
1
  class KT
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -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({})
@@ -6,6 +6,8 @@ require "rspec/eventually"
6
6
  require "pry"
7
7
  require "open3"
8
8
 
9
+ require "spec_support"
10
+
9
11
  HOST = "127.0.0.1"
10
12
  PORT = 1979
11
13
 
@@ -0,0 +1,4 @@
1
+ require "ostruct"
2
+
3
+ class User < OpenStruct
4
+ end
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.2.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-07-16 00:00:00.000000000 Z
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