Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ehcache value need to implement serializable?

Tags:

ehcache

For Ehcache, are there any constraints on the value that needs to be put in to? Like key does the value also need to implement 'serializable'

like image 449
Amol Dixit Avatar asked Apr 08 '16 02:04

Amol Dixit


People also ask

Is it necessary to implement Serializable?

If you want a class object to be serializable, all you need to do it implement the java. io. Serializable interface. Serializable in java is a marker interface and has no fields or methods to implement.

When should a class implement Serializable?

Implement the Serializable interface when you want to be able to convert an instance of a class into a series of bytes or when you think that a Serializable object might reference an instance of your class. Serializable classes are useful when you want to persist instances of them or send them over a wire.

Which classes should implement Serializable?

The Serializable interface must be implemented by the class whose object needs to be persisted. The String class and all the wrapper classes implement the java. io. Serializable interface by default.

What will happen if we don't implement Serializable?

If our class does not implement Serializable interface, or if it is having a reference to a non- Serializable class, then the JVM will throw NotSerializableException . All transient and static fields do not get serialized.


1 Answers

Ehcache puts constraints on key and value types at two different levels:

  • First one is the API, where Element.getKey and Element.getValue, both deprecated, return a Serialiazable. But there are two alternative methods:

    • Element.getObjectKey
    • Element.getObjectValue

    which are the recommended ones anyway, and thus there is no restriction in the end.

  • Second one is the use of multiple tiers. As soon as you use a disk tier, objects have to be represented as byte arrays to be stored on disk and in Java the only standard way of doing that is Serialization.
  • Third one is when you want to enable store by value semantics, where again, the only standard way of doing a deep copy in Java is through Serialization.

That later points are the two cases where your keys and values must implement Serializable.

like image 157
Louis Jacomet Avatar answered Oct 02 '22 07:10

Louis Jacomet