Ehcache3-serializer II

Source: Internet
Author: User
Tags object object serialization

How do I customize serializer?

Only need to implement the Org.ehcache.spi.serialization.Serializer interface of the Serialize, read, equals these 3 methods can be, to see the description of the interface

1 implementation must be thread-safe, you can use the THREADLOCAL;2 implementation must include a constructor containing ClassLoader as a parameter, if the ClassLoader is not NULL, you need to use the ClassLoader to load the class of the object that needs to be deserialized when deserializing 3 The class of the serialized object must be saved; The class of the serialized object must be equal to the class of the deserialized object.

The emphasis here is that after two o'clock, the unity of the ClassLoader guarantees the unity of class, in which case comparisons can be made. Before we introduced the initialization process of serializer, we usually put serializer in Ehcache management, passing in the specified ClassLoader when the serializer is constructed by reflection, and serializer, KeyType, The ValueType class is also loaded by the ClassLoader. When serializing, if the ClassLoader of the instance object's class is inconsistent with the ClassLoader of KeyType, ValueType, the serialization fails (because the corresponding serializer is not found).

If ClassLoader is not specified when constructing CacheManager, the default ClassLoader of Ehcache is used

The following logic allows you to see that the class of KeyType, ValueType, and serializer is loaded by the specified ClassLoader.

Here we use Kryo as the serializer of Ehcache.

What is Kryo?

The official definition of Kryo,kryo is a fast and efficient object graph serialization Framework for Java, simply put, Kryo is a high-performance Java serialization framework.

Org.ehcache.spi.serialization.Serializer requires the implementation of the Serialize, read, equals three methods. When constructing the serializer implementation of Kryo, you can register the class of objects that need to be serialized, which can save a lot of space when using Writeclassandobject. The Kryo instance is non-thread-safe, so you can use the Threadlocal wrapper Kryo. Note that Kryo, if not previously registered, uses the ClassLoader load class that loads the Kryo class if ClassLoader is not specified in Ehcache. This classloader is also the default ClassLoader of Ehcache.

1 @Override2      PublicBytebuffer serialize (Object object)throwsserializerexception {3         byte[] bytes =threadkryo.get (). Writeclassandobject (object);4Bytebuffer Bytebuffer =bytebuffer.wrap (bytes);5         returnBytebuffer;6     }7 8 @Override9      PublicObject Read (Bytebuffer binary)throwsClassNotFoundException, serializerexception {TenObject Read =NULL; OneRead =threadkryo.get (). Readclassandobject (Binary.array ()); A         returnRead; -     } -  the @Override -      Public BooleanEquals (Object object, Bytebuffer binary)throwsClassNotFoundException, serializerexception { -Bytebuffer serialize =serialize (object); -         returnserialize.equals (binary); +}

Ehcache3-serializer II

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.