Advanced Usage

Advanced usage involves individual initialization of every class used by the client. While this approach requires much more code on the part of the developer it is also the most flexible approach. Some environments may need different protocol implementations for different servers and/or may need to implement a custom consistent hashing mechanism to conform to a previous client.

   1:  //Create the end points for two memcached servers
   2:  EndPoint textServerEp = new IPEndPoint(IPAddress.Parse("10.0.0.1"), 11211);
   3:  EndPoint binaryServerEp = new IPEndPoint(IPAddress.Parse("10.0.0.2"), 15455);
   4:   
   5:  //Create a text protocol connection and enable content compression
   6:  TextConnection textCon = new TextConnection(TransportType.Tcp, textServerEp);
   7:  textCon.UseCompression = true;
   8:   
   9:  //Create a binary protocol connection and change the content encoding to UTF16
  10:  BinaryConnection binaryCon = new BinaryConnection(TransportType.Tcp, binaryServerEp);
  11:  binaryCon.PayloadEncoding = Encoding.UTF16;
  12:   
  13:  //Add the connections to a collection
  14:  List<ServerConnection> servers = new List<ServerConnection>();
  15:  servers.Add(textCon);
  16:  servers.Add(binaryCon);
  17:   
  18:  //Create a hashing algorithm instance for the connection provider
  19:  HashAlgorithm hasher = new FNVManaged();
  20:   
  21:  //Create a connection provider with the server collection and hash algorithm
  22:  ConnectionProvider conProvider = new ConnectionProvider(servers, hasher);
  23:  //Change the dead server delay check
  24:  conProvider.PollingInterval = new TimeSpan(0, 1, 0);
  25:   
  26:  //Create the client with the provider and save the cache item
  27:  MemcachedClient client = new MemcachedClient(conProvider);
  28:  ResponseCode response = client.Set("testKey", "this is some test data", DateTime.Now.AddSeconds(15));

Once again, this example performs the exact same operation as the quick start but with much more control over the individual components required to send commands to the servers.

The first few lines define the remote end points of the memcached servers. The connection classes require an end point and transport type so the underlying sockets can be created. In this example we have two servers, one using the text protocol on the default port and a binary connection on an alternate port. The modification of the properties on the connections are not important for this example. They were included as an illustration of possible reasons individual connections would be created.

After the connections are created and configured they are added to a list of ServerConnection objects. The collection of connections does not have to be an IList(Of T) implementation. It can be any class that implements IEnumerable<ServerConnection>, such as the ServerConnectionCollection. The server connections are then passed to the constructor of the ConnectionProvider class. This class, like the ServerConnectionCollection is a convenience class. Custom connection providers can be created by implementing the IConnectionProvider interface.

The client is then created with the specified IConnectionProvider implementation and creates the cache entry. It is not known to the client which server the data is actually sent to since that is the responsibility of the connection provider.

Last edited Aug 23, 2011 at 9:29 PM by JSanford42, version 5

Comments

No comments yet.