lib/dalli/client.rb in dalli-2.1.0 vs lib/dalli/client.rb in dalli-2.2.0
- old
+ new
@@ -10,24 +10,23 @@
#
# Dalli::Client.new(['localhost:11211:10', 'cache-2.example.com:11211:5', '192.168.0.1:22122:5'],
# :threadsafe => true, :failover => true, :expires_in => 300)
#
# servers is an Array of "host:port:weight" where weight allows you to distribute cache unevenly.
- # Both weight and port are optional. If you pass in nil, Dalli will default to 'localhost:11211'.
- # Note that the <tt>MEMCACHE_SERVERS</tt> environment variable will override the servers parameter for use
- # in managed environments like Heroku.
+ # Both weight and port are optional. If you pass in nil, Dalli will use the <tt>MEMCACHE_SERVERS</tt>
+ # environment variable or default to 'localhost:11211' if it is not present.
#
# Options:
# - :namespace - prepend each key with this value to provide simple namespacing.
# - :failover - if a server is down, look for and store values on another server in the ring. Default: true.
# - :threadsafe - ensure that only one thread is actively using a socket at a time. Default: true.
# - :expires_in - default TTL in seconds if you do not pass TTL as a parameter to an individual operation, defaults to 0 or forever
# - :compress - defaults to false, if true Dalli will compress values larger than 100 bytes before
# sending them to memcached.
#
def initialize(servers=nil, options={})
- @servers = env_servers || servers || '127.0.0.1:11211'
+ @servers = servers || env_servers || '127.0.0.1:11211'
@options = normalize_options(options)
@ring = nil
end
#
@@ -197,10 +196,20 @@
ttl ||= @options[:expires_in].to_i
perform(:decr, key, amt.to_i, ttl, default)
end
##
+ # Touch updates expiration time for a given key.
+ #
+ # Returns true if key exists, otherwise nil.
+ def touch(key, ttl=nil)
+ ttl ||= @options[:expires_in].to_i
+ resp = perform(:touch, key, ttl)
+ resp.nil? ? nil : true
+ end
+
+ ##
# Collect the stats for each server.
# Returns a hash like { 'hostname:port' => { 'stat1' => 'value1', ... }, 'hostname2:port' => { ... } }
def stats
values = {}
ring.servers.each do |server|
@@ -231,10 +240,17 @@
private
def ring
@ring ||= Dalli::Ring.new(
Array(@servers).map do |s|
- Dalli::Server.new(s, @options)
+ server_options = {}
+ if s =~ %r{\Amemcached://}
+ uri = URI.parse(s)
+ server_options[:username] = uri.user
+ server_options[:password] = uri.password
+ s = "#{uri.host}:#{uri.port}"
+ end
+ Dalli::Server.new(s, @options.merge(server_options))
end, @options
)
end
def env_servers