How to store memcache objects

Source: Internet
Author: User
Memchache serializes the object, saves memcahce, serializes the value into a byte array, and stores it in the cache. In the following example, the userobject is sequentially converted to the.txt file, and the user is saved to the cache. By comparing the values in the file and cache, we can find that the two are the same. PublicclassUserimplementsSerializable {priv

Memchache serializes the object, saves memcahce, serializes the value into a byte array, and stores it in the cache. In the following example, the userobject is sequentially converted to the.txt file, and the user is saved to the cache. By comparing the values in the file and cache, we can find that the two are the same. Public class User implements Serializable {priv

Memchache serializes the object and saves it

Memcahce serializes values into byte arrays and stores them in the cache.

In the following example, the userobject is sequentially converted to the.txt file, and the user is saved to the cache by comparing the values in the file and cache.
The two are the same.

public class User implements Serializable{    private String name;    private String address;    public User(String name, String address) {        super();        this.name = name;        this.address = address;    }}
Public static void main (String [] args) throws InterruptedException, ExecutionException, FileNotFoundException, IOException {MemcachedClient mcc = null; try {// locally connect to the Memcached service mcc = new MemcachedClient (new InetSocketAddress ("127.0.0.1", 11211); System. out. println ("Connection to server sucessful. ");} catch (Exception ex) {System. out. println (ex. getMessage ();} User u = new User ("junwang", "qingdao city "); objectOutputStream oos = new ObjectOutputStream (new FileOutputStream (new File ("a.txt"); oos. writeObject (u); Future fo = mcc. set ("myuser", 5*60*1000, u); // view the storage status System. out. println ("set status:" + fo. get (); // output value System. out. println ("myuser value in cache-" + mcc. get ("myuser"); // close the connection to mcc. shutdown ();}

View Source Code

View the source code and find that the object is serialized and then saved. Proves our above conjecture.

BaseSerializingTranscoder.javaprotected byte[] serialize(Object o) {    if (o == null) {      throw new NullPointerException("Can't serialize null");    }    byte[] rv=null;    ByteArrayOutputStream bos = null;    ObjectOutputStream os = null;    try {      bos = new ByteArrayOutputStream();      os = new ObjectOutputStream(bos);      os.writeObject(o);      os.close();      bos.close();      rv = bos.toByteArray();    } catch (IOException e) {      throw new IllegalArgumentException("Non-serializable object", e);    } finally {      CloseUtil.close(os);      CloseUtil.close(bos);    }    return rv;  }
Conclusion

The stored values are serialized into byte arrays and then saved.

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.