Class MemCache::Server
In: vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb
Parent: Object

This class represents a memcached server instance.

Methods

alive?   close   inspect   new   socket  

Constants

CONNECT_TIMEOUT = 0.25   The amount of time to wait to establish a connection with a memcached server. If a connection cannot be established within this time limit, the server will be marked as down.
RETRY_DELAY = 30.0   The amount of time to wait before attempting to re-establish a connection with a server that is marked dead.

Attributes

host  [R]  The host the memcached server is running on.
port  [R]  The port the memcached server is listening on.
retry  [R]  The time of next retry if the connection is dead.
status  [R]  A text status string describing the state of the server.
weight  [R]  The weight given to the server.

Public Class methods

Create a new MemCache::Server object for the memcached instance listening on the given host and port, weighted by the given weight.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb, line 749
749:     def initialize(memcache, host, port = DEFAULT_PORT, weight = DEFAULT_WEIGHT)
750:       raise ArgumentError, "No host specified" if host.nil? or host.empty?
751:       raise ArgumentError, "No port specified" if port.nil? or port.to_i.zero?
752: 
753:       @memcache = memcache
754:       @host   = host
755:       @port   = port.to_i
756:       @weight = weight.to_i
757: 
758:       @multithread = @memcache.multithread
759:       @mutex = Mutex.new
760: 
761:       @sock   = nil
762:       @retry  = nil
763:       @status = 'NOT CONNECTED'
764:     end

Public Instance methods

Check whether the server connection is alive. This will cause the socket to attempt to connect if it isn‘t already connected and or if the server was previously marked as down and the retry time has been exceeded.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb, line 779
779:     def alive?
780:       !!socket
781:     end

Close the connection to the memcached server targeted by this object. The server is not considered dead.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb, line 819
819:     def close
820:       @mutex.lock if @multithread
821:       @sock.close if @sock && !@sock.closed?
822:       @sock   = nil
823:       @retry  = nil
824:       @status = "NOT CONNECTED"
825:     ensure
826:       @mutex.unlock if @multithread
827:     end

Return a string representation of the server object.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb, line 769
769:     def inspect
770:       "<MemCache::Server: %s:%d [%d] (%s)>" % [@host, @port, @weight, @status]
771:     end

Try to connect to the memcached server targeted by this object. Returns the connected socket object on success or nil on failure.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb, line 787
787:     def socket
788:       @mutex.lock if @multithread
789:       return @sock if @sock and not @sock.closed?
790: 
791:       @sock = nil
792: 
793:       # If the host was dead, don't retry for a while.
794:       return if @retry and @retry > Time.now
795: 
796:       # Attempt to connect if not already connected.
797:       begin
798:         @sock = timeout CONNECT_TIMEOUT do
799:           TCPSocket.new @host, @port
800:         end
801:         if Socket.constants.include? 'TCP_NODELAY' then
802:           @sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
803:         end
804:         @retry  = nil
805:         @status = 'CONNECTED'
806:       rescue SocketError, SystemCallError, IOError, Timeout::Error => err
807:         mark_dead err.message
808:       end
809: 
810:       return @sock
811:     ensure
812:       @mutex.unlock if @multithread
813:     end

[Validate]