Skip to content

Commit

Permalink
Merge pull request arthurnn#126 from joe1chen/fix-memcached-rails-inc…
Browse files Browse the repository at this point in the history
…rement-decrement

Fix Memcached::Rails increment and decrement to match ActiveSupport's memcache implementation.
  • Loading branch information
evan committed Jun 14, 2013
2 parents b39c46b + 6eaacc2 commit d632ce8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/memcached/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,19 @@ def set_servers(servers)
end
super
end

def increment(name, amount = 1, options = nil)
response = super(name, amount)
response ? response.to_i : nil
rescue
nil
end

def decrement(name, amount = 1, options = nil)
response = super(name, amount)
response ? response.to_i : nil
rescue
nil
end
end
end
26 changes: 26 additions & 0 deletions test/unit/rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ def test_clear
assert_equal nil, @cache.read("x")
end

def test_increment
rand_key = "rand-key-#{rand}"
assert_equal nil, @cache.increment(rand_key)

start = 10
@cache.write rand_key, start.to_s, { raw: true }

assert_equal start, @cache.read(rand_key, { raw: true }).to_i
assert_equal start+1, @cache.increment(rand_key)

assert_equal start+1+5, @cache.increment(rand_key, 5)
end

def test_decrement
rand_key = "rand-key-#{rand}"
assert_equal nil, @cache.decrement(rand_key)

start = 10
@cache.write rand_key, start.to_s, { raw: true }

assert_equal start, @cache.read(rand_key, { raw: true }).to_i
assert_equal start-1, @cache.decrement(rand_key)

assert_equal start-1-5, @cache.decrement(rand_key, 5)
end

private

def key
Expand Down

0 comments on commit d632ce8

Please sign in to comment.