References to Ehcache content and deep copy of Java

Source: Internet
Author: User
Tags object serialization

The project uses Ehcache as a buffer between the program and the database, the cache object is modified during use, such as Plan.setlangsymbol (), where the cache is used, and the data taken is modified, so the guess is caused by a shallow reference to the cache.

In fact, StackOverflow also mentions this issueCache.get () returns deep copy of element? 》

As for how to do deep Copy,stackoverflow also mentions theCopying cached map<string, list> object into temporary map<string, List> ; Object, after experimentation, confirms that guava's Maps.newhashmap (MAP) Tool class method is also shallow copy.

One way is to iterate over the contents of the object, assigning each property to a new, rebuilt object.

In addition, there is a simpler way, "A Java deep Clone" example uses the object serialization characteristics to complete, see the following code:

  /*** This method makes a ' deep clone ' of any object it is given. */   Public StaticObject Deepclone (Object object) {Try{bytearrayoutputstream BAOs=NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BAOs);      Oos.writeobject (object); Bytearrayinputstream Bais=NewBytearrayinputstream (Baos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (Bais); returnOis.readobject (); }    Catch(Exception e) {e.printstacktrace (); return NULL; }  }} /*** These classes implement Serializable so we can write them out and * read them back in as a stream of bytes.*/classPersonImplementsSerializable {String firstName, lastName;  Address address;  PublicPerson (String firstName, String lastName, address address) { This. FirstName =FirstName;  This. LastName =LastName;  This. Address =address; }   PublicString toString () {StringBuilder sb=NewStringBuilder (); Sb.append ("First Name:" + firstName + "\ n"); Sb.append ("Last Name:" + lastName + "\ n"); Sb.append ("Street:" + address.street + "\ n"); returnsb.tostring (); }}

Very simple!

References to Ehcache content and deep copy of Java

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.