kt 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/kt.rb +54 -0
  3. data/lib/kt/version.rb +1 -1
  4. data/spec/kt_spec.rb +46 -0
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e51d9769475943af5267c8d3e9cf7497bc4830dd
4
- data.tar.gz: d9d0385cec3ed60994fe58af7796b946ae8ac4c7
3
+ metadata.gz: 886faae0652f12144d48cccf4c45e05cc8e8882c
4
+ data.tar.gz: 2e9840117521cbecb24ab10efe95e6f39affbd64
5
5
  SHA512:
6
- metadata.gz: 86663c41ca8c8a04c7db442d41a2fead8446a1d016f803ca379d62f9b110c20a4b209c113c1c907c40e47d665de812183f3ebf25616f70694ce1765107735eca
7
- data.tar.gz: 26c05b1d767681bcdd70603d006b8ec9874560f5b9bdf34a2b315bdba875e1552af3043af2c09dda784000d620446759996c1a833a7815d2d64481e14a71c4c0
6
+ metadata.gz: fcd913d071081a5067a9c153d25dbf1df187702bc3c36a24611bb847ce0e2bd4889e55d27c84fb68c988641cb4d175abff8bd84ab72b3e6ffb6867e717f89230
7
+ data.tar.gz: 6585cee4ce5a1ca7abe3ddc8f050c9d2df4585f5437efd7228419fc2192cf8864e6a71a990404a8da1d8083a090d428d30e7a03f79754d9e0b575f6f603c18b3
data/lib/kt.rb CHANGED
@@ -102,6 +102,60 @@ class KT
102
102
  return res
103
103
  end
104
104
 
105
+ # ttl returns the time to live for a key in seconds
106
+ # if key does not exist, it returns -2
107
+ # if key exists but no ttl is set, it returns -1
108
+ # if key exists and it has a ttl, it returns the number of seconds remaining
109
+ def ttl(key)
110
+ req = [
111
+ KT::KV.new("key", key),
112
+ ]
113
+
114
+ status, res_body = do_rpc("/rpc/get", req)
115
+
116
+ case status
117
+ when 200
118
+ xt_pos = res_body.index{|kv| kv.key == "xt"}
119
+ if xt_pos != nil
120
+ expire_time = res_body[xt_pos].value.to_f
121
+ [(expire_time - Time.now.to_f).to_i, 0].max
122
+ else
123
+ -1
124
+ end
125
+ when 450
126
+ -2
127
+ else
128
+ raise_error(res_body)
129
+ end
130
+ end
131
+
132
+ # pttl returns the time to live for a key in milliseconds
133
+ # if key does not exist, it returns -2
134
+ # if key exists but no ttl is set, it returns -1
135
+ # if key exists and it has a ttl, it returns the number of milliseconds remaining
136
+ def pttl(key)
137
+ req = [
138
+ KT::KV.new("key", key),
139
+ ]
140
+
141
+ status, res_body = do_rpc("/rpc/get", req)
142
+
143
+ case status
144
+ when 200
145
+ xt_pos = res_body.index{|kv| kv.key == "xt"}
146
+ if xt_pos != nil
147
+ expire_time = res_body[xt_pos].value.to_f
148
+ [expire_time - Time.now.to_f, 0].max
149
+ else
150
+ -1
151
+ end
152
+ when 450
153
+ -2
154
+ else
155
+ raise_error(res_body)
156
+ end
157
+ end
158
+
105
159
  # set stores the data at key
106
160
  def set(key, value, expire: nil)
107
161
  req = [
@@ -1,3 +1,3 @@
1
1
  class KT
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -204,6 +204,52 @@ describe KT do
204
204
  end
205
205
  end
206
206
 
207
+ describe "ttl" do
208
+ it "returns -2 if key does not exist" do
209
+ expect(@kt.ttl("ttl/not/existing/key")).to eql(-2)
210
+ end
211
+
212
+ it "returns -1 if key exists but no ttl is set" do
213
+ @kt.set("ttl/no/expire/key", "1")
214
+ expect(@kt.ttl("ttl/no/expire/key")).to eql(-1)
215
+ end
216
+
217
+ it "returns ttl if key exists and it has ttl set" do
218
+ @kt.set("ttl/test/key/1", "1", expire: 3)
219
+ expect(@kt.ttl("ttl/test/key/1")).to eql(2)
220
+ end
221
+
222
+ it "returns ttl if key exists and it has ttl set to 5" do
223
+ @kt.set("ttl/test/key/2", "1", expire: 5)
224
+ expect(@kt.ttl("ttl/test/key/2")).to eql(4)
225
+ end
226
+ end
227
+
228
+ describe "pttl" do
229
+ it "returns -2 if key does not exist" do
230
+ expect(@kt.pttl("ttl/not/existing/key")).to eql(-2)
231
+ end
232
+
233
+ it "returns -1 if key exists but no ttl is set" do
234
+ @kt.set("ttl/no/expire/key", "1")
235
+ expect(@kt.pttl("ttl/no/expire/key")).to eql(-1)
236
+ end
237
+
238
+ it "returns ttl if key exists and it has ttl set" do
239
+ @kt.set("ttl/test/key/1", "1", expire: 3)
240
+ ttl = @kt.pttl("ttl/test/key/1")
241
+ expect(ttl < 3).to be_truthy
242
+ expect(ttl > 2).to be_truthy
243
+ end
244
+
245
+ it "returns ttl if key exists and it has ttl set to 5" do
246
+ @kt.set("ttl/test/key/2", "1", expire: 5)
247
+ ttl = @kt.pttl("ttl/test/key/2")
248
+ expect(ttl < 5).to be_truthy
249
+ expect(ttl > 4).to be_truthy
250
+ end
251
+ end
252
+
207
253
  describe "binary" do
208
254
  it "sets binary and gets it" do
209
255
  @kt.set_bulk({"Café" => "foo"})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Teodor Pripoae