Skip to content

Commit

Permalink
Merge branch 'bug-fix_memcache_compatibility'
Browse files Browse the repository at this point in the history
  • Loading branch information
mwynholds committed Jan 14, 2011
2 parents 6397baf + e6ec905 commit a2d227c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.bundle
.idea
Gemfile.lock
tmtags
1 change: 1 addition & 0 deletions lib/dalli.rb
Expand Up @@ -4,6 +4,7 @@
require 'dalli/socket'
require 'dalli/version'
require 'dalli/options'
require 'dalli/memcache_client_compatibility'

unless ''.respond_to?(:bytesize)
class String
Expand Down
12 changes: 12 additions & 0 deletions lib/dalli/client.rb
Expand Up @@ -24,6 +24,18 @@ class Client
def initialize(servers=nil, options={})
@servers = env_servers || servers || 'localhost:11211'
@options = { :expires_in => 0 }.merge(options)
self.extend(Dalli::Client::MemcacheClientCompatibility) if Dalli::Client.compatibility_mode
end

##
# Turn on compatibility mode, which mixes in methods in memcache_client_compatibility.rb
# This value is set to true in memcache-client.rb.
def self.compatibility_mode
@compatibility_mode ||= false
end

def self.compatibility_mode=(compatibility_mode)
@compatibility_mode = compatibility_mode
end

#
Expand Down
55 changes: 1 addition & 54 deletions lib/dalli/memcache-client.rb
@@ -1,54 +1 @@
require 'dalli/client'

class Dalli::Client
module MemcacheClientCompatibility

def initialize(*args)
Dalli.logger.error("Starting Dalli in memcache-client compatibility mode")
super(*args)
end

def set(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use set(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"

end

def add(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use add(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"
end

def replace(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use replace(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"
end

# Dalli does not unmarshall data that does not have the marshalled flag set so we need
# to unmarshall manually any marshalled data originally put in memcached by memcache-client.
# Peek at the data and see if it looks marshalled.
def get(key, options = nil)
value = super(key, options)
if value && value.is_a?(String) && !options && value.size > 2 &&
bytes = value.unpack('cc') && bytes[0] == 4 && bytes[1] == 8
return Marshal.load(value) rescue value
end
value
end

def delete(key)
super(key) ? "DELETED\r\n" : "NOT_DELETED\r\n"
end
end

include MemcacheClientCompatibility unless $TESTING
end
Dalli::Client.compatibility_mode = true
52 changes: 52 additions & 0 deletions lib/dalli/memcache_client_compatibility.rb
@@ -0,0 +1,52 @@
class Dalli::Client

module MemcacheClientCompatibility

def initialize(*args)
Dalli.logger.error("Starting Dalli in memcache-client compatibility mode")
super(*args)
end

def set(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use set(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"

end

def add(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use add(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"
end

def replace(key, value, ttl = nil, options = nil)
if options == true || options == false
Dalli.logger.error("Dalli: please use replace(key, value, ttl, :raw => boolean): #{caller[0]}")
options = { :raw => options }
end
super(key, value, ttl, options) ? "STORED\r\n" : "NOT_STORED\r\n"
end

# Dalli does not unmarshall data that does not have the marshalled flag set so we need
# to unmarshall manually any marshalled data originally put in memcached by memcache-client.
# Peek at the data and see if it looks marshalled.
def get(key, options = nil)
value = super(key, options)
if value && value.is_a?(String) && !options && value.size > 2 &&
bytes = value.unpack('cc') && bytes[0] == 4 && bytes[1] == 8
return Marshal.load(value) rescue value
end
value
end

def delete(key)
super(key) ? "DELETED\r\n" : "NOT_DELETED\r\n"
end
end

end
14 changes: 11 additions & 3 deletions test/test_compatibility.rb
@@ -1,13 +1,15 @@
require 'helper'
require 'dalli/memcache-client'

class TestCompatibility < Test::Unit::TestCase

def setup
require 'dalli/memcache-client'
end

context 'dalli in memcache-client mode' do

should 'handle old raw flag to set/add/replace' do
memcached do |dc|
dc.extend(Dalli::Client::MemcacheClientCompatibility)
assert_equal "STORED\r\n", dc.set('abc', 123, 5, true)
assert_equal '123', dc.get('abc', true)

Expand All @@ -16,10 +18,16 @@ class TestCompatibility < Test::Unit::TestCase

assert_equal "STORED\r\n", dc.replace('abc', 456, 5, false)
assert_equal 456, dc.get('abc', false)

assert_equal "DELETED\r\n", dc.delete('abc')
assert_equal "NOT_DELETED\r\n", dc.delete('abc')
end
end

end

def teardown
Dalli::Client.compatibility_mode = false
end

end

0 comments on commit a2d227c

Please sign in to comment.