Practical application of memcached

Source: Internet
Author: User

On the official website of memcached, you can find such a document: howto.txt

Howto ===== basic example: =================== lets say you have 3 servers. server 1 and Server 2 have 3 GB of Spaceand Server 3 has 2 GB of space for cache. here is how I wocould set upmy client. ----------------------------------------------------------------------------- import COM. danga. memcached. *; public class myclass {// create a static client as most installonly need // a single instance
// Create a memcachedclient client for interacting with the memcachedserver protected static memcachedclient MCC = new memcachedclient (); // set up connection pool once at class load static {// server list and weights
// Because a client (such as your web application) may access multiple servers.
// When using the Java client, the default port of the memcached service for Windows is 11211 string [] servers = {"server1.mydomain.com: 1624", "server2.mydomain.com: 1624", "server3.mydomain.com: 1624 "}; integer [] weights = {3, 3, 2}; // grab an instance of our connection pool sockiopool pool = sockiopool. getinstance (); // set the servers and the weights pool. setservers (servers); pool. setweights (weights); // set some TCP settings // disable Nagle // set the read timeout to 3 secs // and don't set a connect timeout pool. setnagle (false); pool. setsocketto (3000); pool. setsocketconnectto (0); // initialize the connection pool. initialize ();} // from here on down, you can call any of the client CILS public static void examples (){
// Store data to the Cache Server MCC. Set ("foo", "this is a test string ");
// Number of slave servers string bar = MCC. get ("foo "). tostring () ;}----------------------------------------------------------------------------------- multi-client example: ============================== if you need to support multiple clients (I. e. java, PHP, Perl, etc .) you need to make a few changes when you are setting things up: ----------------------------------------------------------- // use a compatible hashing algorithm pool. sethashingalg (sockiopool. new_compat_hash); ------------------------------------------------------------------- serialization: ================== for Java "native types", which include: Must client will by default not use Java serialization, and insteadwill serialize using the primitive values to save space. for other Java objects, you have 2 options to serialize the Java objects. one is make sure the class implements serializable in order to be able to be stored with default object Transcoder provided by this client; the other alternative is to write your own Transcoder to do the serialization and deserialization by yourself, the following is simple example: Creating package COM. schoner. memcached; import Java. io. bytearrayinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. io. outputstream;/*** {@ link objecttranscoder} is the default Transcoder used to handle the * serialization and deserialization in memcached operations. ** @ author xingen Wang * @ see abstracttranscoder * @ see Transcoder */public class objecttranscoder extends abstracttranscoder {/** (non-javadoc) ** @ see COM. schoner. memcached. transcoder # decode (inputstream) */public object decode (final inputstream input) throws ioexception {object OBJ = NULL; objectinputstream OIS = new objectinputstream (input); try {OBJ = Ois. readobject ();} catch (classnotfoundexception e) {Throw new ioexception (E. getmessage ();} Ois. close (); Return OBJ;}/** (non-javadoc) ** @ see * COM. schoner. memcached. abstracttranscoder # encode (Java. io. outputstream, * Java. lang. object) */Public void encode (final outputstream output, final object) throws ioexception {objectoutputstream OOS = new objectoutputstream (output); OOS. writeobject (object); OOS. close ();} public object decode (byte [] input) {object OBJ = NULL; try {objectinputstream OIS = new objectinputstream (New bytearrayinputstream (input); OBJ = Ois. readobject (); ois. close ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (classnotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}return OBJ ;}}after that, You shoshould set Transcoder to your client: New memcachedclient MC = new memcachedclient (); MC. settranscoder (New yourtranscoder (); Using I wocould also recommend that if possible, classes shocould insteadimplement externalizable as opposed to serializable or write The Transcoder by your self. this allows theauthor of the class to define how objects of that class shouldserialize. in practice at meetup.com, we saw a 60% callback ction in the sizeof our serialized objects by doing this. this means less data to eat upcache space and less data to transfer over the network. binary Protocol: ====================== in Scholar's implementation, binary protocol for memcached has been implemented, and due to our test performance increased about 5% overall, but you have to use memcached 1.4 + in the server side to support this features. the following code snipets shows how to use this feature: Using MC = new memcachedclient (True, true); using UDP protocol: ============== in Scholar's implementation, UDP protocol for memcached is also supported. while dueto our test, the performance is not that good as TCP protocol, we are still working on performance tuning. in our latest implementation, UDP protocol works in asynchronized mode. we recommend user memcached 1.4.4 + when UDP protocol is used. using MC = new memcachedclient (false, false); --------------------------------------------------------------------------- other :==== see the Java docs.

In the first example above:

If examples () is executed for the first time, objects with the key as 'foo' will be stored on the server.

If the client completes the execution, change examples ()

Public static void examples (){

// MCC. Set ("foo", "this is a test string ");
String bar = MCC. Get ("foo"). tostring ();}

That is, the value of key = 'foo' is not set, but the number is directly obtained.



In actual development, it is generally used before and after interacting with the database.
To query data:
// 1) Number of retrieved data from the Cache Server
// 2) If it is obtained, it will be returned directly and no longer retrieved from the database. The number of retrieved data from the database.
// 3) after obtaining the data, store the data on the cache server.


// For update operations:
// 1) update the database directly
// 2) update the data on the cache server.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.