Hessian serialization protocol + memcached cache access

Source: Internet
Author: User
Tags php memcached

I'm afraid no one knows about memcached! Hessian is a remote call mechanism.
, Similar to Web service, but it uses its own serialization protocol.
So why should we combine Hessian serialization protocol with memcached to implement caching?
?
People who have experience using memcached will learn about the performance of PHP + memcached.
Is the best, Java + memcached performance is poor, the main reason is that Java serialization mechanism is very slow.
I did a simple test.
,
A userdata class has a string attribute, a date attribute, and a double attribute, which are serialized 1 million times using Java and Hessian respectively.
The speed of Hessian serialization is twice faster than that of Java, and the number of bytes after Hessian serialization is also twice that of Java. Because I serialized this during the test.
The serialized results are not uploaded to the network.
Therefore, the actual performance of Hessian should be better!
Since Hessian serialization protocols are better than Java, the performance of the memcached client depends heavily on Object serialization. Therefore, I decided to use Hessian to serialize this part of the cache implementation.
The memcached client I use is danga. The memcached package mainly changes the get method and Set Method of memcachedclient. In the set method, the serialization of Hessian is called instead:
Bytearrayoutputstream Bos = new bytearrayoutputstream ();
// Modify the previous serialization code:
// (New objectoutputstream (BOS). writeobject (value );
// The modified serialization code:

Serializebyhessian (Bos, value );
Val = Bos. tobytearray ();
The serializebyhessian method is as follows:
Protected void serializebyhessian (outputstream OS, object) throws ioexception {
Export acthessianoutput out = new hessian2output (OS );;
Serializerfactory = getserializerfactory ();
Out. setserializerfactory (serializerfactory );
Out. startreply ();
Out. writeobject (object );
Out. completereply ();
Out. Flush ();
}
The get method mainly modifies the readobject method of the contextobjectinputstream class called by this method:
The readobjectoverride method is overwritten in contextobjectinputstream:

Protected object readobjectoverride () throws ioexception, classnotfoundexception {
Bytearrayinputstream is = new bytearrayinputstream (bytes );
Clhessian2input in = new clhessian2input (is, this. mloader );
In. setserializerfactory (getserializerfactory ());
Int code = in. Read (); // "R"
Int major = in. Read (); // % 26gt; = 2
Int minor = in. Read (); // 0
Object value = in. readobject ();
Is. Close ();
Return value;
}
Because my framework
Yes
Because it is based on osgi, I have overloaded hessian2input and passed classloader as a parameter. Otherwise, Hessian cannot find the class during deserialization. For example
If you do not use the osgi framework, clhessian2input in = new
Clhessian2input (is, this. Mloader); this line of code can be used directly: hessian2input in = new
Hessian2input (is );
This completes the modification.
I changed the serialization protocol of memcached client to Hessian. There is also another system architecture.
Of
The reason is that my service layer logic is implemented using Java + spring + osgi, while the web layer is implemented using PHP, and the communication between the two is already implemented using Hessian.
. Therefore, some cached data is set to the memcached server through Java at the service layer. memcached PHP can be used in PHP.
Read from the client. (PhP memcached
Client I use memcached-client. PHP, rather than PhP extensions, can modify memcached-client. PHP serialization
Mechanism)

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.