memcached-issues to keep Java entities in the memcached

Source: Internet
Author: User

Today, in the form of a code instance, summarize the issues that you should be aware of when you save Java entities to memcached:

Memcached Tool Class Code:

Package Com.ghj.packageoftool;import Java.util.date;import Com.danga.memcached.memcachedclient;import com.danga.memcached.sockiopool;/** * MemCached Tool class * * @author Gaohuanjie */public class Memcachedutils {private static Me Mcachedclient memcachedclient;static {/************************************ configuration memcached**************************         /Sockiopool Sockiopool = Sockiopool.getinstance (); Sockiopool.setservers (New string[]{"127.0.0.1:11211"});//Set memcached server address Sockiopool.setweights (new integer[]{3}) ;  Set each memcached server weight sockiopool.setfailover (true);         Whether to connect to another memcached server when a memcached server fails.    Sockiopool.setinitconn (10);     Number of connections established for each server at initialization sockiopool.setminconn (10);    Each server establishes the minimum number of connections sockiopool.setmaxconn (100);  Establish maximum number of connections per server Sockiopool.setmaintsleep (30);    Self-examination thread cycle to work, the time of each sleep Sockiopool.setnagle (false); The socket parameter, if true, is not buffered when writing the data and is immediately sent out. The rule of TCP is that before a packet is sent, the sender of the packet waits for the remote receiver to confirm that it has received the last packet sent.This method closes the socket cache-the packet is ready to be sent out immediately.  Sockiopool.setsocketto (3000); The socket block reads the data timeout Sockiopool.setalivecheck (true);//sets whether to check whether the memcached server is invalid Sockiopool.setmaxidle (1000*30* 30);  Set maximum processing time Sockiopool.setsocketconnectto (0);  Control of timeout when connection is established sockiopool.initialize ();         Initialize the connection pool if (memcachedclient = = null) {memcachedclient = new memcachedclient (); }}private memcachedutils () {}/** * Adds a key-value pair to the cache and sets a time-out for that key-value pair (that is, how long after that key-value pair is removed from the memcached memory cache, such as: New Date (1000*10), is removed from the memcached memory cache after 10 seconds). * * @author Gaohuanjie */public Static Boolean Add (String key, Object value, Date expire) {try {return memcachedclient.ad D (key, value, expire);} catch (Exception e) {e.printstacktrace (); return false;}} /** * The corresponding value in the memcached memory cache management system according to the key * * @author Gaohuanjie */public static Object get (String key) {try {return memcachedcli Ent.get (key);} catch (Exception e) {e.printstacktrace (); return null;}}}
Test the class code of the Main method:
Package Com.ghj.packageofclient;import Java.util.date;import Com.ghj.packageoftool.memcachedutils;import Com.ghj.packageofvo.user;public class Client{public static void Main (string[] args) {Memcachedutils.add ("User", new User ("Liunannan", "[email protected]"), new Date (1000*60));//Add a serialized object to memcached User user = (user) ( Memcachedutils.get ("user")); System.err.println ("Username:" +user.getusername () + ", Password:" + User.getpassword ());}}
Java Entity code:
Package com.ghj.packageofvo;/** * User Business Bean *  * @author Gao Yingjie */public class User{private string Username;private string Password;public User (String userName, string password) {super (); this.username = Username;this.password = password;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
Run the main method and you will see the following exception:

A bit of Java-based people know: The reason why this problem occurs is because the Java entity User class is not instantiated, so the user class should be changed to the following code:

Package Com.ghj.packageofvo;import java.io.serializable;/** * User Business Bean *  * @author Gao Yingjie */public class user implements Serializable{private static final Long serialversionuid = -3371451210123762490l;private string Username;private string p Assword;public User (String userName, string password) {super (); this.username = Username;this.password = password;} Public String GetUserName () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
0 min Download Engineering code

memcached-issues to keep Java entities in the memcached

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.