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.