Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #437 from koraktor/code-quality

 Reduce code duplication in Dalli::Server (Part 2)
  • Loading branch information...
commit dfa1ba6ccc223344cf6447a06c3e1217a01c55f1 2 parents dfd1bfb + 4c9deb3
@mperham authored
Showing with 14 additions and 17 deletions.
  1. +14 −17 lib/dalli/server.rb
View
31 lib/dalli/server.rb
@@ -311,26 +311,27 @@ def flush(ttl)
generic_response
end
- def decr(key, count, ttl, default)
+ def decr_incr(opcode, key, count, ttl, default)
expiry = default ? sanitize_ttl(ttl) : 0xFFFFFFFF
default ||= 0
(h, l) = split(count)
(dh, dl) = split(default)
- req = [REQUEST, OPCODES[:decr], key.bytesize, 20, 0, 0, key.bytesize + 20, 0, 0, h, l, dh, dl, expiry, key].pack(FORMAT[:decr])
+ req = [REQUEST, OPCODES[opcode], key.bytesize, 20, 0, 0, key.bytesize + 20, 0, 0, h, l, dh, dl, expiry, key].pack(FORMAT[opcode])
write(req)
body = generic_response
- body ? longlong(*body.unpack('NN')) : body
+ body ? body.unpack('Q>').first : body
+ end
+
+ def decr(key, count, ttl, default)
+ decr_incr :decr, key, count, ttl, default
end
def incr(key, count, ttl, default)
- expiry = default ? sanitize_ttl(ttl) : 0xFFFFFFFF
- default ||= 0
- (h, l) = split(count)
- (dh, dl) = split(default)
- req = [REQUEST, OPCODES[:incr], key.bytesize, 20, 0, 0, key.bytesize + 20, 0, 0, h, l, dh, dl, expiry, key].pack(FORMAT[:incr])
- write(req)
- body = generic_response
- body ? longlong(*body.unpack('NN')) : body
+ decr_incr :incr, key, count, ttl, default
+ end
+
+ def write_append_prepend(opcode, key, value)
+ write_generic [REQUEST, OPCODES[opcode], key.bytesize, 0, 0, 0, value.bytesize + key.bytesize, 0, 0, key, value].pack(FORMAT[opcode])
end
def write_generic(bytes)
@@ -351,11 +352,11 @@ def noop
end
def append(key, value)
- write_generic [REQUEST, OPCODES[:append], key.bytesize, 0, 0, 0, value.bytesize + key.bytesize, 0, 0, key, value].pack(FORMAT[:append])
+ write_append_prepend :append, key, value
end
def prepend(key, value)
- write_generic [REQUEST, OPCODES[:prepend], key.bytesize, 0, 0, 0, value.bytesize + key.bytesize, 0, 0, key, value].pack(FORMAT[:prepend])
+ write_append_prepend :prepend, key, value
end
def stats(info='')
@@ -574,10 +575,6 @@ def split(n)
[n >> 32, 0xFFFFFFFF & n]
end
- def longlong(a, b)
- (a << 32) | b
- end
-
REQUEST = 0x80
RESPONSE = 0x81
Please sign in to comment.
Something went wrong with that request. Please try again.