An issue where get comes out as null after using Ehcache to put an object into the cache

Source: Internet
Author: User

An issue where get comes out as null after using Ehcache to put an object into the cacheProblem

When you previously used Ehcache to cache data, the saved value was based on Java's underlying type data, and when you recently found the object that saved the custom type, the Get () element is either null or thrown when you use the element's GetValue () net.sf.ehcache.CacheException: Value xxx is not Serializableexception.

Solution Solutions
  1. Ehcache The object must be a serializable (Serializable) type when the object is put, that is, to implement thejava.io.Serializable。 This is because Ehcache is serialized when it is put on the object.
    Official document Description of the Ehcache put method:

    Put

    Void put (element element) throws Illegalargumentexception,illegalstateexception, Cacheexception
    Put An element in the cache.
    resets the access statistics on the element, which would is the case if it had previously been gotten from a cache, an D is now being put back.

    Also notifies the Cacheeventlistener that:

    The element is put, but only if the element is actually put.
    If the element exists in the cache, which an update had occurred, even if the element would be expired if it is reques Ted
    Parameters:
    Element-an object. If Serializable It can fully participate in replication and the Diskstore.

    Throws:
    Illegalstateexception-if The cache is not status.status_alive
    Illegalargumentexception-if the Eleme NT is null
    Cacheexception

In addition: the Flush () method of Ehcache is useful for serializing the cache to a file and caching the configuration Diskpersistent= "true". If the put does not have a serialized object and then Flush (), get () from the cache will get Null (the serialized object is not supported and cannot be persisted to the file). If there is no flush () after the put, you can get () to the cached element, but the exception is reported when the GetValue () of the element is called to fetch the cached object net.sf.ehcache.CacheException: Value xxx is not Serializable .

An issue where get comes out as null after using Ehcache to put an object into the cache

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.