lib/memcache.rb in jruby-memcache-client-1.7.0 vs lib/memcache.rb in jruby-memcache-client-1.7.1
- old
+ new
@@ -1,16 +1,16 @@
require 'java'
require 'base64'
-require File.dirname(__FILE__) + '/java/memcached-dev_2.0.2.jar'
+require File.dirname(__FILE__) + '/java/java_memcached-release_2.5.1.jar'
class MemCache
- include_class 'com.meetup.memcached.MemcachedClient'
- include_class 'com.meetup.memcached.SockIOPool'
- include_class 'com.meetup.memcached.Logger'
+ java_import 'com.danga.MemCached.MemCachedClient'
+ java_import 'com.danga.MemCached.SockIOPool'
+ java_import 'com.danga.MemCached.Logger'
- VERSION = '1.7.0'
+ VERSION = '1.7.1'
##
# Default options for the cache object.
DEFAULT_OPTIONS = {
@@ -83,17 +83,26 @@
@servers = [@servers].flatten
else
raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
end
+ # Normalizing the server(s) so they all have a port number.
+
+ @servers = @servers.map do |server|
+ server =~ /(.+):(\d+)/ ? server : "#{server}:#{DEFAULT_PORT}"
+ end
+
+ Logger.getLogger('com.meetup.memcached.MemcachedClient').setLevel(opts[:log_level])
+ Logger.getLogger('com.meetup.memcached.SockIOPool').setLevel(opts[:log_level])
+
opts = DEFAULT_OPTIONS.merge opts
@namespace = opts[:namespace] || opts["namespace"]
@pool_name = opts[:pool_name] || opts["pool_name"]
@readonly = opts[:readonly] || opts["readonly"]
- @client = MemcachedClient.new(@pool_name)
+ @client = MemCachedClient.new(@pool_name)
@client.error_handler = opts[:error_handler] if opts[:error_handler]
@client.primitiveAsString = true
@client.sanitizeKeys = false
@@ -117,21 +126,29 @@
@pool.failover = opts[:pool_use_failover]
@pool.failback = opts[:pool_use_failback]
@pool.aliveCheck = opts[:pool_use_alive]
@pool.nagle = opts[:pool_use_nagle]
+ # public static final int NATIVE_HASH = 0;
+ # // native String.hashCode();
+ # public static final int OLD_COMPAT_HASH = 1;
+ # // original compatibility hashing algorithm (works with other clients)
+ # public static final int NEW_COMPAT_HASH = 2;
+ # // new CRC32 based compatibility hashing algorithm (works with other clients)
+ # public static final int CONSISTENT_HASH = 3;
+ # // MD5 Based -- Stops thrashing when a server added or removed
+ @pool.hashingAlg = opts[:pool_hashing_algorithm]
+
# __method methods have been removed in jruby 1.5
- @pool.java_send :initialize rescue @pool.initialize__method
+ @pool.java_send :initialize rescue @pool.initialize__method
end
- Logger.getLogger('com.meetup.memcached.MemcachedClient').setLevel(opts[:log_level])
- Logger.getLogger('com.meetup.memcached.SockIOPool').setLevel(opts[:log_level])
end
def reset
@pool.shut_down
- @pool.java_send :initialize rescue @pool.initialize__method
+ @pool.java_send :initialize rescue @pool.initialize__method
end
##
# Returns the servers that the client has been configured to
# use. Injects an alive? method into the string so it works with the
@@ -161,13 +178,21 @@
# cache. Retrieves the raw value if the raw parameter is set.
def get(key, raw = false)
value = @client.get(make_cache_key(key))
return nil if value.nil?
unless raw
- marshal_bytes = java.lang.String.new(value).getBytes(MARSHALLING_CHARSET)
- decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
- value = Marshal.load(decoded)
+ begin
+ marshal_bytes = java.lang.String.new(value).getBytes(MARSHALLING_CHARSET)
+ decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
+ value = Marshal.load(decoded)
+ rescue
+ value = case value
+ when /^\d+\.\d+$/ then value.to_f
+ when /^\d+$/ then value.to_i
+ else value
+ end
+ end
end
value
end
alias :[] :get
@@ -181,13 +206,21 @@
values_j = @client.getMulti(keys)
values_j.to_a.each {|kv|
k,v = kv
next if v.nil?
unless raw
- marshal_bytes = java.lang.String.new(v).getBytes(MARSHALLING_CHARSET)
- decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
- v = Marshal.load(decoded)
+ begin
+ marshal_bytes = java.lang.String.new(v).getBytes(MARSHALLING_CHARSET)
+ decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
+ v = Marshal.load(decoded)
+ rescue
+ v = case v
+ when /^\d+\.\d+$/ then v.to_f
+ when /^\d+$/ then v.to_i
+ else v
+ end
+ end
end
values[k] = v
}
values
end
@@ -248,24 +281,22 @@
##
# Increments the value associated with the key by a certain amount.
def incr(key, amount = 1)
raise MemCacheError, "Update of readonly cache" if @readonly
- value = get(key) || 0
- value += amount
- set key, value
- value
+ value = @client.incr(make_cache_key(key), amount)
+ return nil if value == "NOT_FOUND\r\n"
+ return value.to_i
end
##
# Decrements the value associated with the key by a certain amount.
def decr(key, amount = 1)
raise MemCacheError, "Update of readonly cache" if @readonly
- value = get(key) || 0
- value -= amount
- set key, value
- value
+ value = @client.decr(make_cache_key(key),amount)
+ return nil if value == "NOT_FOUND\r\n"
+ return value.to_i
end
##
# Clears the cache.
def flush_all
@@ -303,9 +334,10 @@
def expiration(expiry)
java.util.Date.new((Time.now.to_i + expiry) * 1000)
end
def marshal_value(value)
+ return value if value.kind_of?(Numeric)
encoded = Base64.encode64(Marshal.dump(value))
marshal_bytes = encoded.to_java_bytes
java.lang.String.new(marshal_bytes, MARSHALLING_CHARSET)
end
end