AdoccaMemcache 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/am_memcache.rb +59 -56
  3. metadata +1 -1
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rubygems'
4
4
  require 'hoe'
5
5
  require './lib/adocca_memcache.rb'
6
6
 
7
- Hoe.new('AdoccaMemcache', '0.1.7') do |p|
7
+ Hoe.new('AdoccaMemcache', '0.1.8') do |p|
8
8
  p.rubyforge_name = 'adocca-plugins'
9
9
  p.author = 'adocca Entertainment AB'
10
10
  p.summary = 'A client library to simplify using memcached with Ruby on Rails projects.'
data/lib/am_memcache.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'socket'
2
- require 'thread'
3
2
  require 'digest/sha1'
4
3
  require 'timeout'
5
4
  require 'monitor'
@@ -71,7 +70,7 @@ module Adocca
71
70
  @namespace = opts[:namespace]
72
71
  @readonly = opts[:readonly]
73
72
  if ActionController::Base.allow_concurrency
74
- @mutex = Mutex.new
73
+ @mutex = Monitor.new
75
74
  else
76
75
  @mutex = NullMutex.new
77
76
  end
@@ -79,6 +78,16 @@ module Adocca
79
78
  @buckets = []
80
79
  end
81
80
 
81
+ def self.log_error(e)
82
+ if defined?(RAILS_DEFAULT_LOGGER)
83
+ RAILS_DEFAULT_LOGGER.error(e)
84
+ RAILS_DEFAULT_LOGGER.error(PP.pp(e.backtrace, ""))
85
+ else
86
+ puts e
87
+ pp e.backtrace
88
+ end
89
+ end
90
+
82
91
  # Return a string representation of the cache object.
83
92
  def inspect
84
93
  sprintf("<MemCache: %s servers, %s buckets, ns: %p, ro: %p>",
@@ -128,7 +137,7 @@ module Adocca
128
137
  rval = nil
129
138
  @mutex.synchronize do
130
139
  begin
131
- response, sock, server = sock_send_command(cache_key, "get #{cache_key}")
140
+ response, sock, server = sock_send_command(cache_key, "get #{cache_key}", true)
132
141
  return :MemCache_no_such_entry if response =~ /^END/
133
142
 
134
143
  v, cache_key, flags, bytes = response.split(/ /)
@@ -136,11 +145,11 @@ module Adocca
136
145
 
137
146
  sock.gets
138
147
  sock.gets
139
- rescue MemCacheError => e
140
- raise e
141
148
  rescue Exception => err
142
149
  server.mark_dead(err.message)
143
- raise MemCacheError, err.message
150
+
151
+ self.class.log_error(err)
152
+ return :MemCache_no_such_entry
144
153
  end
145
154
  end
146
155
 
@@ -151,7 +160,8 @@ module Adocca
151
160
  begin
152
161
  Marshal.load(rval)
153
162
  rescue ArgumentError, TypeError => err
154
- raise MemCacheError, err.message
163
+ self.class.log_error(err)
164
+ return :MemCache_no_such_entry
155
165
  end
156
166
  end
157
167
  end
@@ -168,10 +178,7 @@ module Adocca
168
178
  #
169
179
  def inc(key, amount = 1)
170
180
  cache_key = make_cache_key(key)
171
- rval = nil
172
- @mutex.synchronize do
173
- rval = send_command(cache_key, "incr #{cache_key} #{amount}")
174
- end
181
+ rval = send_command(cache_key, "incr #{cache_key} #{amount}")
175
182
  rval = rval.strip.to_i if rval =~ /^\d+/
176
183
  rval
177
184
  end
@@ -181,10 +188,7 @@ module Adocca
181
188
  #
182
189
  def dec(key, amount = 1)
183
190
  cache_key = make_cache_key(key)
184
- rval = nil
185
- @mutex.synchronize do
186
- rval = send_command(cache_key, "decr #{cache_key} #{amount}")
187
- end
191
+ rval = send_command(cache_key, "decr #{cache_key} #{amount}")
188
192
  rval = rval.strip.to_i if rval =~ /^\d+/
189
193
  rval
190
194
  end
@@ -204,9 +208,7 @@ module Adocca
204
208
  else
205
209
  marshaled_value = Marshal.dump(value)
206
210
  end
207
- @mutex.synchronize do
208
- send_command(cache_key, "add #{cache_key} 0 #{expiry} #{marshaled_value.size}\r\n" + marshaled_value) == ADD_STORED
209
- end
211
+ send_command(cache_key, "add #{cache_key} 0 #{expiry} #{marshaled_value.size}\r\n" + marshaled_value) == ADD_STORED
210
212
  end
211
213
 
212
214
  # Add an entry to the cache.
@@ -218,18 +220,14 @@ module Adocca
218
220
  else
219
221
  marshaled_value = Marshal.dump(value)
220
222
  end
221
- @mutex.synchronize do
222
- send_command(cache_key, "set #{cache_key} 0 #{expiry} #{marshaled_value.size}\r\n" + marshaled_value)
223
- end
223
+ send_command(cache_key, "set #{cache_key} 0 #{expiry} #{marshaled_value.size}\r\n" + marshaled_value)
224
224
  end
225
225
 
226
226
  # Remove an entry from the cache.
227
227
  # return true if it is deleted
228
228
  def delete(key, expiry = 0)
229
229
  cache_key = make_cache_key(key)
230
- @mutex.synchronize do
231
- send_command(cache_key, "delete #{cache_key} #{expiry}")
232
- end
230
+ send_command(cache_key, "delete #{cache_key} #{expiry}")
233
231
  end
234
232
 
235
233
  # Reset the connection to all memcache servers. This should be called if
@@ -247,7 +245,7 @@ module Adocca
247
245
  @mutex.synchronize do
248
246
 
249
247
  begin
250
- response, sock, server = sock_send_command(cache_key, "get #{cache_key}")
248
+ response, sock, server = sock_send_command(cache_key, "get #{cache_key}", true)
251
249
 
252
250
  return false if response =~ /^END/
253
251
 
@@ -257,7 +255,8 @@ module Adocca
257
255
  sock.gets
258
256
  rescue Exception => err
259
257
  server.mark_dead(err.message)
260
- raise MemCacheError, err.message
258
+ self.class.log_error(err)
259
+ return false
261
260
  end
262
261
 
263
262
  true
@@ -271,18 +270,16 @@ module Adocca
271
270
  @servers.each do |server|
272
271
 
273
272
  sock = server.socket
274
- if sock.nil?
275
- raise MemCacheError, "No connection to server"
276
- end
277
-
278
- begin
279
- timeout(LIVELINESS_TIMEOUT) do
280
- sock.write "flush_all\r\n"
281
- sock.gets
273
+ unless sock.nil?
274
+ begin
275
+ timeout(LIVELINESS_TIMEOUT) do
276
+ sock.write "flush_all\r\n"
277
+ sock.gets
278
+ end
279
+ rescue Exception => err
280
+ server.mark_dead(err.message)
281
+ self.class.log_error(err)
282
282
  end
283
- rescue Exception => err
284
- server.mark_dead(err.message)
285
- raise MemCacheError, err.message
286
283
  end
287
284
  end
288
285
  end
@@ -363,7 +360,7 @@ module Adocca
363
360
 
364
361
  # The amount of time to wait before attempting to re-establish a
365
362
  # connection with a server that is marked dead.
366
- RETRY_DELAY = 10
363
+ RETRY_DELAY = 60
367
364
 
368
365
  # The host the memcached server is running on.
369
366
  attr_reader :host
@@ -430,6 +427,7 @@ module Adocca
430
427
  @status = "CONNECTED"
431
428
  rescue Exception => err
432
429
  self.mark_dead(err.message)
430
+ MemCache.log_error(err)
433
431
  end
434
432
  end
435
433
  end
@@ -499,25 +497,30 @@ module Adocca
499
497
  # Will send a command to the proper server for this key
500
498
  # and return the response, socket and server
501
499
  #
502
- def sock_send_command(key, command)
503
- raise MemCacheError, "No active servers" unless self.active?
504
- raise MemCacheError, "Update of readonly cache" if @readonly
505
- server = get_server_for_key(key)
506
-
507
- sock = server.socket
508
- if sock.nil?
509
- raise MemCacheError, "No connection to server"
510
- end
511
-
512
- response = nil
513
- begin
514
- timeout(LIVELINESS_TIMEOUT) do
515
- sock.write "#{command}\r\n"
516
- [sock.gets, sock, server]
500
+ def sock_send_command(key, command, squash_errors = false)
501
+ @mutex.synchronize do
502
+ server = nil
503
+ begin
504
+ raise MemCacheError, "No active servers" unless self.active?
505
+ raise MemCacheError, "Update of readonly cache" if @readonly
506
+ server = get_server_for_key(key)
507
+
508
+ sock = server.socket
509
+ raise MemCacheError, "No connection to server" if sock.nil?
510
+
511
+ response = nil
512
+
513
+ timeout(LIVELINESS_TIMEOUT) do
514
+ sock.write "#{command}\r\n"
515
+ [sock.gets, sock, server]
516
+ end
517
+
518
+ rescue Exception => err
519
+ server.mark_dead(err.message) if server
520
+ self.class.log_error(err)
521
+ raise err unless squash_errors
522
+ return err.message
517
523
  end
518
- rescue Exception => err
519
- server.mark_dead(err.message)
520
- raise MemCacheError, err.message
521
524
  end
522
525
  end
523
526
 
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: AdoccaMemcache
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.7
6
+ version: 0.1.8
7
7
  date: 2007-02-12 00:00:00 +01:00
8
8
  summary: A client library to simplify using memcached with Ruby on Rails projects.
9
9
  require_paths: