Class: EventMachine::Protocols::Memcache::TenaciousMC

Inherits:
Client
  • Object
show all
Defined in:
lib/evented_memcache_client/tenacious.rb

Overview

TenaciousMC is a “tenacious” memcache client connection. When the underlying connection is lost, it attempts to reacquire it automatically.

Constant Summary

Constants included from Connectable

Connectable::ALL_MESSAGES, Connectable::ARGS_AND_BODY_MESSAGES, Connectable::ARG_AND_BODY_MESSAGES, Connectable::ARG_MESSAGES, Connectable::BODY_MESSAGES, Connectable::HEADER_REGEX, Connectable::NO_ARG_MESSAGES

Instance Method Summary collapse

Methods included from Connectable

#handle, #io_per_sec, #parse_header, #parse_incoming_data, #post_init, #receive_data, #receive_message, #remote_endpoint, #set_handler, #start_closing, #to_s

Methods included from Sender

#book_it, #cas, #delete, #get, #send_to_peer, #stats, #value

Constructor Details

#initialize(*args) ⇒ TenaciousMC

Create a new “tenacious” connection, which will attempt to reacquire a connection to the server if it is lost.

Note that when you’re using EventMachine, you won’t be calling TenaciousMC::new yourself - EM does it for you when a connection is established.

First argument as per EventMachine::Protocols::Memcache::Client. Ie., callback hash, module or handler object.

Second argument is hash of options governing TenaciousMC’s behavior:

{ :host => 'somewhere.com', :port=>12345, :interval => 5 }

If the options hash includes a :eager key, and its value is true, then when the connection opens, TenaciousMC will immediately send a ‘get’ to the server to fetch data, using the value of the :key option as the key fetched.

So a typical invocation:

EventMachine::run {
  EventMachine::connect('over.there',
                        12345,
                        EventMachine::Protocols::Memcache::TenaciousMC,
                        MyHandlerModule,
                        {:host => 'over.there', :port => 12345})
}


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/evented_memcache_client/tenacious.rb', line 65

def initialize(*args)
  @handle_arg   = args[0]
  options       = args[1]
  @host         = options[:host] || 'localhost'
  @port         = options[:port]
  raise "No port option is set" unless @port
  @interval     = options[:interval] || 5
  @eager        = options[:eager] || false
  @key          = options[:key] || nil
  super(*args)
end

Instance Method Details

#connection_completedObject

Called from EM::Connection after the connection is established.



78
79
80
81
82
83
# File 'lib/evented_memcache_client/tenacious.rb', line 78

def connection_completed
  super
  if @eager && @key
    self.get(:key => @key)
  end
end

#unbindObject

Called from EM::Connection when the connection is dead.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/evented_memcache_client/tenacious.rb', line 86

def unbind
  super
  if @interval > 0
    EventMachine::add_timer(@interval) do
      EventMachine::connect(@host,
                            @port,
                            self.class,
                            @handle_arg,
                            {
                              :host => @host,
                              :port => @port,
                              :interval => @interval
                            })
    end
  end
end