quetzall-cloud_cache 1.1.6 → 1.2.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.
Files changed (3) hide show
  1. data/lib/cloud_cache.rb +38 -12
  2. data/test/cache_tests.rb +166 -140
  3. metadata +2 -2
data/lib/cloud_cache.rb CHANGED
@@ -54,6 +54,7 @@ module ActiveSupport
54
54
  if (http_method == :put)
55
55
  req = Net::HTTP::Put.new(uri.path)
56
56
  req.body = body unless body.nil?
57
+ #puts 'BODY SIZE=' + req.body.length.to_s
57
58
  elsif (http_method == :post)
58
59
  req = Net::HTTP::Post.new(uri.path)
59
60
  if !parameters.nil?
@@ -66,6 +67,9 @@ module ActiveSupport
66
67
  end
67
68
  else
68
69
  req = Net::HTTP::Get.new(uri.path)
70
+ if !parameters.nil?
71
+ req.set_form_data(parameters)
72
+ end
69
73
  end
70
74
  headers.each_pair do |k, v|
71
75
  req[k] = v
@@ -93,7 +97,9 @@ module ActiveSupport
93
97
  run_http(:get, command_name, command_path)
94
98
  end
95
99
 
96
- def put(key, val, seconds_to_store=0, raw=false)
100
+ def put(key, val, options={})
101
+ seconds_to_store = options[:expires_in] || options[:ttl]
102
+ raw = options[:raw]
97
103
  #puts 'putting ' + val.to_s + ' to key=' + key
98
104
  seconds_to_store = 0 if seconds_to_store.nil?
99
105
  if raw
@@ -106,9 +112,9 @@ module ActiveSupport
106
112
  run_http(:put, "PUT", key, data, nil, extra_headers)
107
113
  end
108
114
 
109
-
110
- def get_multi(keys, raw=false)
115
+ def get_multi(keys, options={})
111
116
  return {} if keys.size == 0
117
+ raw = options[:raw]
112
118
  kj = keys.to_json
113
119
  #puts "keys.to_json = " + kj
114
120
  extra_headers = {"keys" => kj }
@@ -155,7 +161,8 @@ module ActiveSupport
155
161
  values
156
162
  end
157
163
 
158
- def get(key, raw=false)
164
+ def get(key, options={})
165
+ raw = options[:raw]
159
166
  begin
160
167
  data = run_http(:get, "GET", key)
161
168
  rescue Net::HTTPServerException
@@ -173,7 +180,7 @@ module ActiveSupport
173
180
 
174
181
  # returns the value as an int.
175
182
  def get_i(key)
176
- val = get(key, true)
183
+ val = get(key, :raw=>true)
177
184
  return nil if val.nil?
178
185
  return val.to_i
179
186
  end
@@ -213,12 +220,23 @@ module ActiveSupport
213
220
 
214
221
  def write(name, value, options={})
215
222
  super
216
- put(name, value, options[:expires_in], options[:raw])
223
+ put(name, value, options)
217
224
  end
218
225
 
219
226
  def delete(name, options = nil)
220
227
  super
221
- run_http(:delete, "DELETE", name)
228
+ begin
229
+ run_http(:delete, "DELETE", name)
230
+ rescue Net::HTTPServerException => ex
231
+ puts 'CAUGHT ' + ex.response.inspect
232
+ case ex.response
233
+ when Net::HTTPNotFound
234
+ return false
235
+ else
236
+ raise ex
237
+ end
238
+ end
239
+ true
222
240
  end
223
241
 
224
242
  def remove(name, options=nil)
@@ -235,7 +253,7 @@ module ActiveSupport
235
253
  end
236
254
 
237
255
  def exists?(key, options = nil)
238
- x = get(key, true)
256
+ x = get(key, :raw=>true)
239
257
  return !x.nil?
240
258
  end
241
259
 
@@ -247,13 +265,21 @@ module ActiveSupport
247
265
  v
248
266
  end
249
267
 
250
- def increment(key, val=1)
251
- ret = run_http(:post, "POST", key + "/incr", nil, {"val"=>val})
268
+ def increment(key, val=1, options={})
269
+ headers = {"val"=>val}
270
+ if options[:set_if_not_found]
271
+ headers["x-cc-set-if-not-found"] = options[:set_if_not_found]
272
+ end
273
+ ret = run_http(:post, "POST", key + "/incr", nil, headers)
252
274
  ret.to_i
253
275
  end
254
276
 
255
- def decrement(key, val=1)
256
- ret = run_http(:post, "POST", key + "/decr", nil, {"val"=>val})
277
+ def decrement(key, val=1, options={})
278
+ headers = {"val"=>val}
279
+ if options[:set_if_not_found]
280
+ headers["x-cc-set-if-not-found"] = options[:set_if_not_found]
281
+ end
282
+ ret = run_http(:post, "POST", key + "/decr", nil, headers)
257
283
  ret.to_i
258
284
  end
259
285
 
data/test/cache_tests.rb CHANGED
@@ -20,196 +20,222 @@ class CacheTests < Test::Unit::TestCase
20
20
  @cache = ActiveSupport::Cache::CloudCache.new(props['access_key'], props['secret_key'])
21
21
  end
22
22
 
23
-
24
23
  def teardown
25
24
  @cache.shutdown unless @cache.nil?
26
25
  end
27
26
 
28
27
  def test_auth
29
- @cache.auth()
30
- end
28
+ @cache.auth()
29
+ end
30
+
31
+ def test_bad_auth
32
+
33
+ temp = @cache.secret_key
34
+ @cache.secret_key = "badkey"
35
+
36
+ assert_raise Net::HTTPServerException do
37
+ test_basic_ops
38
+ end
39
+
40
+ @cache.secret_key = temp
41
+ end
42
+
43
+ def test_basic_ops
44
+ to_put = "I am a testing string. Take me apart and put me back together again."
45
+ @cache.put("s1", to_put)
46
+
47
+ sleep(1)
48
+
49
+ response = @cache.get("s1")
50
+ assert_equal(to_put, response)
31
51
 
32
- def test_bad_auth
52
+ end
33
53
 
34
- temp = @cache.secret_key
35
- @cache.secret_key = "badkey"
54
+ def test_not_exists
55
+ assert_nil @cache.get("does_not_exist")
56
+ end
36
57
 
37
- assert_raise Net::HTTPServerException do
38
- test_basic_ops
39
- end
58
+ def test_delete
59
+ to_put = "I am a testing string. Take me apart and put me back together again."
60
+ @cache.put("s1", to_put)
40
61
 
41
- @cache.secret_key = temp
42
- end
62
+ sleep(1)
43
63
 
44
- def test_basic_ops
45
- to_put = "I am a testing string. Take me apart and put me back together again."
46
- @cache.put("s1", to_put, 0);
64
+ response = @cache.get("s1")
65
+ assert_equal(to_put, response)
47
66
 
48
- sleep(1)
67
+ @cache.delete("s1")
49
68
 
50
- response = @cache.get("s1")
51
- assert_equal(to_put, response)
69
+ response = @cache.get("s1")
70
+ assert_nil(response)
71
+ end
52
72
 
53
- end
73
+ def test_expiry
74
+ to_put = "I am a testing string. Take me apart and put me back together again."
75
+ @cache.put("s1", to_put, :ttl=>2);
76
+ sleep(4)
77
+ response = @cache.get("s1")
78
+ assert_nil(response)
54
79
 
55
- def test_not_exists
56
- assert_nil @cache.get("does_not_exist")
57
- end
80
+ @cache.write("s1", to_put, :expires_in=>2);
81
+ sleep(4)
82
+ response = @cache.get("s1")
83
+ assert_nil(response)
84
+ end
58
85
 
59
- def test_delete
60
- to_put = "I am a testing string. Take me apart and put me back together again."
61
- @cache.put("s1", to_put, 0)
86
+ def test_list_keys
87
+ @cache.put("k1", "v2", :expires_in=>15)
88
+ sleep 1
89
+ keys = @cache.list_keys
90
+ puts("PRINTING KEYS:")
91
+ for key in keys
92
+ puts key
93
+ end
94
+ haskey = keys.index("k1")
95
+ assert_not_nil(haskey)
96
+ end
62
97
 
63
- sleep(1)
98
+ def test_counters
99
+ val = 0
100
+ key = "counter1" # should add a test for a key with a slash
101
+ @cache.put(key, val, :ttl=>50000, :raw=>true)
102
+ 10.times do
103
+ val = @cache.increment(key)
104
+ puts 'val=' + val.to_s
105
+ end
106
+ assert_equal(10, val)
64
107
 
65
- response = @cache.get("s1")
66
- assert_equal(to_put, response)
108
+ # get as normal int now
109
+ get_val = @cache.get_i(key)
110
+ assert_equal(10, get_val)
67
111
 
68
- @cache.delete("s1");
112
+ 10.times do
113
+ val = @cache.decrement(key)
114
+ end
115
+ assert_equal(0, val)
69
116
 
70
- response = @cache.get("s1")
71
- assert_nil(response)
72
- end
117
+ # One more to make sure it stays at 0
118
+ val = @cache.decrement(key)
119
+ assert_equal(0, val)
73
120
 
74
- def test_expiry
75
- to_put = "I am a testing string. Take me apart and put me back together again."
76
- @cache.put("s1", to_put, 2);
121
+ end
77
122
 
78
- sleep(4)
123
+ def test_flush
124
+ x = @cache.flush
125
+ assert_equal('[]', x)
126
+ end
79
127
 
80
- response = @cache.get("s1")
81
- assert_nil(response)
82
- end
128
+ def test_stats
129
+ x = @cache.stats
130
+ puts x
131
+ end
83
132
 
84
- def test_list_keys
85
- @cache.put("k1", "v2", 1000)
86
- sleep 1
87
- keys = @cache.list_keys
88
- puts("PRINTING KEYS:")
89
- for key in keys
90
- puts key
91
- end
92
- haskey = keys.index("k1")
93
- assert_not_nil(haskey)
94
- end
133
+ def test_get_multi_raw
134
+ @cache.remove("m1") rescue false
135
+ @cache.remove("m2") rescue false
136
+ @cache.remove("m3") rescue false
137
+ @cache.remove("m4") rescue false
95
138
 
96
- def test_counters
97
- val = 0
98
- key = "counter1"
99
- @cache.put(key, val, 50000, true)
100
- 10.times do
101
- val = @cache.increment(key)
102
- end
103
- assert_equal(10, val)
139
+ @cache.put("m1", "v1", :ttl=>500, :raw=>true)
140
+ @cache.put("m2", "v2", :ttl=>500, :raw=>true)
104
141
 
105
- # get as normal int now
106
- get_val = @cache.get_i(key)
107
- assert_equal(10, get_val)
142
+ kz = Array["m1", "m2", "m3"]
143
+ vz = @cache.get_multi(kz, :raw=>true)
108
144
 
109
- 10.times do
110
- val = @cache.decrement(key)
111
- end
112
- assert_equal(0, val)
145
+ assert_equal("v1", vz["m1"])
146
+ assert_equal("v2", vz["m2"])
147
+ assert_nil(vz["m3"])
113
148
 
114
- # One more to make sure it stays at 0
115
- val = @cache.decrement(key)
116
- assert_equal(0, val)
117
149
 
118
- end
150
+ end
119
151
 
120
- def test_flush
121
- x = @cache.flush
122
- assert_equal('[]', x)
123
- end
124
-
125
- def test_stats
126
- x = @cache.stats
127
- puts x
128
- end
152
+ def test_get_multi
129
153
 
130
- def test_get_multi_raw
131
- @cache.remove("m1") rescue false
132
- @cache.remove("m2") rescue false
133
- @cache.remove("m3") rescue false
134
- @cache.remove("m4") rescue false
154
+ kz = []
155
+ vz = @cache.get_multi(kz)
156
+ assert vz.size == 0
135
157
 
136
- @cache.put("m1", "v1", 500, true)
137
- @cache.put("m2", "v2", 500, true)
158
+ kz = ["nothere"]
159
+ vz = @cache.get_multi(kz)
160
+ assert vz.size == 0
138
161
 
139
- kz = Array["m1", "m2", "m3"]
140
- vz = @cache.get_multi(kz, true)
162
+ @cache.remove("m1") rescue false
163
+ @cache.remove("m2") rescue false
164
+ @cache.remove("m3") rescue false
165
+ @cache.remove("m4") rescue false
141
166
 
142
- assert_equal("v1", vz["m1"]);
143
- assert_equal("v2", vz["m2"]);
144
- assert_nil(vz["m3"]);
167
+ @cache.put("m1", "v1", :ttl=>500, :raw=>false)
168
+ @cache.put("m2", "v2", :ttl=>500, :raw=>false)
169
+ @cache.put("m4", MyClass.new("Travis", 10), :ttl=>500, :raw=>false)
170
+
171
+ kz = ["m1", "m2", "m3", "m4"]
172
+ vz = @cache.get_multi(kz)
173
+
174
+ assert_equal("v1", vz["m1"]);
175
+ assert_equal("v2", vz["m2"]);
176
+ assert_nil(vz["m3"]);
177
+ assert_equal("Travis", vz["m4"].name)
178
+ assert_equal(10, vz["m4"].age)
145
179
 
180
+ @cache.put("m3", MyClass.new("Leroy", 3), :ttl=>500, :raw=>false)
181
+
182
+ kz = ["m1", "m2", "m3", "m4"]
183
+ vz = @cache.get_multi(kz)
146
184
 
147
- end
185
+ assert_equal("v1", vz["m1"]);
186
+ assert_equal("v2", vz["m2"]);
187
+ assert_equal("Leroy", vz["m3"].name)
188
+ assert_equal(3, vz["m3"].age)
189
+ assert_equal("Travis", vz["m4"].name)
190
+ assert_equal(10, vz["m4"].age)
148
191
 
149
- def test_get_multi
150
192
 
151
- kz = []
152
- vz = @cache.get_multi(kz)
153
- assert vz.size == 0
193
+ end
154
194
 
155
- kz = ["nothere"]
156
- vz = @cache.get_multi(kz)
157
- assert vz.size == 0
195
+ def test_big
196
+ s = random_string(100000)
197
+ @cache.put("s1", s)
158
198
 
159
- @cache.remove("m1") rescue false
160
- @cache.remove("m2") rescue false
161
- @cache.remove("m3") rescue false
162
- @cache.remove("m4") rescue false
199
+ s2 = @cache.get("s1")
163
200
 
164
- @cache.put("m1", "v1", 500, false)
165
- @cache.put("m2", "v2", 500, false)
166
- @cache.put("m4", MyClass.new("Travis", 10), 500, false)
201
+ assert_equal(s, s2)
202
+ end
167
203
 
168
- kz = ["m1", "m2", "m3", "m4"]
169
- vz = @cache.get_multi(kz)
204
+ def random_string(length=10)
205
+ chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
206
+ password = ''
207
+ length.times { password << chars[rand(chars.size)] }
208
+ password
209
+ end
170
210
 
171
- assert_equal("v1", vz["m1"]);
172
- assert_equal("v2", vz["m2"]);
173
- assert_nil(vz["m3"]);
174
- assert_equal("Travis", vz["m4"].name)
175
- assert_equal(10, vz["m4"].age)
211
+ def test_usage
212
+ usage = @cache.usage
213
+ assert_kind_of(Numeric, usage)
214
+ end
176
215
 
177
- @cache.put("m3", MyClass.new("Leroy", 3), 500, false)
178
216
 
179
- kz = ["m1", "m2", "m3", "m4"]
180
- vz = @cache.get_multi(kz)
217
+ def test_set_if_not_found
218
+ key = "sinf"
219
+ @cache.delete(key)
181
220
 
182
- assert_equal("v1", vz["m1"]);
183
- assert_equal("v2", vz["m2"]);
184
- assert_equal("Leroy", vz["m3"].name)
185
- assert_equal(3, vz["m3"].age)
186
- assert_equal("Travis", vz["m4"].name)
187
- assert_equal(10, vz["m4"].age)
188
-
189
-
190
- end
191
-
192
- def test_big
193
- s = random_string(1500)
194
- @cache.put("s1", s)
195
-
196
- s2 = @cache.get("s1")
221
+ assert_raise Net::HTTPServerException do
222
+ @cache.increment(key, 1)
223
+ end
197
224
 
198
- assert_equal(s, s2)
199
- end
225
+ val = 3
226
+ @cache.increment(key, 1, :set_if_not_found=>val)
200
227
 
201
- def random_string(length=10)
202
- chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
203
- password = ''
204
- length.times { password << chars[rand(chars.size)] }
205
- password
206
228
  end
207
229
 
208
- def test_usage
209
- usage = @cache.usage
210
- assert_kind_of(Numeric, usage)
230
+ def test_failing_data
231
+ fname = "fail_message.txt"
232
+ return if !File.exists?(fname)
233
+ file = File.new fname
234
+ result = file.read # JSON.parse(file.read)
235
+ file.close
236
+ puts result.inspect
237
+ @cache.put("bigolmsg", result)
211
238
  end
212
239
 
213
240
 
214
-
215
241
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quetzall-cloud_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-13 00:00:00 -07:00
12
+ date: 2009-09-09 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15