lib/dalli/client.rb in dalli-2.0.5 vs lib/dalli/client.rb in dalli-2.1.0
- old
+ new
@@ -1,5 +1,7 @@
+require 'digest/md5'
+
# encoding: ascii
module Dalli
class Client
##
@@ -81,11 +83,11 @@
values
end
end
def fetch(key, ttl=nil, options=nil)
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
val = get(key, options)
if val.nil? && block_given?
val = yield
add(key, val, ttl, options)
end
@@ -102,37 +104,37 @@
# Returns:
# - nil if the key did not exist.
# - false if the value was changed by someone else.
# - true if the value was successfully updated.
def cas(key, ttl=nil, options=nil, &block)
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
(value, cas) = perform(:cas, key)
value = (!value || value == 'Not found') ? nil : value
if value
newvalue = block.call(value)
perform(:set, key, newvalue, ttl, cas, options)
end
end
def set(key, value, ttl=nil, options=nil)
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
perform(:set, key, value, ttl, 0, options)
end
##
# Conditionally add a key/value pair, if the key does not already exist
# on the server. Returns true if the operation succeeded.
def add(key, value, ttl=nil, options=nil)
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
perform(:add, key, value, ttl, options)
end
##
# Conditionally add a key/value pair, only if the key already exists
# on the server. Returns true if the operation succeeded.
def replace(key, value, ttl=nil, options=nil)
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
perform(:replace, key, value, ttl, options)
end
def delete(key)
perform(:delete, key)
@@ -170,11 +172,11 @@
# Note that the ttl will only apply if the counter does not already
# exist. To increase an existing counter and update its TTL, use
# #cas.
def incr(key, amt=1, ttl=nil, default=nil)
raise ArgumentError, "Positive values only: #{amt}" if amt < 0
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
perform(:incr, key, amt.to_i, ttl, default)
end
##
# Decr subtracts the given amount from the counter on the memcached server.
@@ -190,11 +192,11 @@
# Note that the ttl will only apply if the counter does not already
# exist. To decrease an existing counter and update its TTL, use
# #cas.
def decr(key, amt=1, ttl=nil, default=nil)
raise ArgumentError, "Positive values only: #{amt}" if amt < 0
- ttl ||= @options[:expires_in]
+ ttl ||= @options[:expires_in].to_i
perform(:decr, key, amt.to_i, ttl, default)
end
##
# Collect the stats for each server.
@@ -255,11 +257,15 @@
end
def validate_key(key)
raise ArgumentError, "key cannot be blank" if !key || key.length == 0
key = key_with_namespace(key)
- raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
+ if key.length > 250
+ namespace_length = @options[:namespace] ? @options[:namespace].size : 0
+ max_length_before_namespace = 212 - namespace_length
+ key = "#{key[0, max_length_before_namespace]}:md5:#{Digest::MD5.hexdigest(key)}"
+ end
return key
end
def key_with_namespace(key)
@options[:namespace] ? "#{@options[:namespace]}:#{key}" : key
@@ -272,10 +278,14 @@
def normalize_options(opts)
if opts[:compression]
Dalli.logger.warn "DEPRECATED: Dalli's :compression option is now just :compress => true. Please update your configuration."
opts[:compress] = opts.delete(:compression)
end
- opts[:expires_in] ||= 0
+ begin
+ opts[:expires_in] = opts[:expires_in].to_i if opts[:expires_in]
+ rescue NoMethodError
+ raise ArgumentError, "cannot convert :expires_in => #{opts[:expires_in].inspect} to an integer"
+ end
opts
end
end
end