To cache an object in Redis

Source: Internet
Author: User

1,java Configure Redis Client

public class Redisclient implements serializable{private Jedis Jedis;
    Private Jedispool Jedispool;
    Shardedjedis Shardedjedis;

    Private Shardedjedispool Shardedjedispool;
        Public redisclient () {initialpool ();
        Initialsharepool ();
        Shardedjedis = Shardedjedispool.getresource ();
    Jedis = Jedispool.getresource (); /** * Initialize non-slice pool/private void Initialpool () {//Pool basic configuration jedispoolconfig config = new Jed
        Ispoolconfig ();
        Config.setmaxactive (20);
        Config.setmaxidle (5);
        Config.setmaxwait (10001);

        Config.settestonborrow (FALSE);
    Jedispool = new Jedispool (config, Enums.LINUX_HOST.getValue (), 6379);  /** * Initialize slicing pool/private void Initialsharepool () {//pool base configuration jedispoolconfig config = new
        Jedispoolconfig ();
        Config.setmaxactive (20);
        Config.setmaxidle (5);
        Config.setmaxwait (10001);
Config.settestonborrow (FALSE);        Slave connection list<jedisshardinfo> Shardinfos = new arraylist<jedisshardinfo> ();
        Shardinfos.add (New Jedisshardinfo (Enums.LINUX_HOST.getValue (), 6379, 1000));
    Construction Pool Shardedjedispool = new Shardedjedispool (config, shardinfos); }
}

2, the mutual transformation of array and object

1, the object implements the serializable interface in order to serialize and deserialize


2, the code that converts objects and arrays into each other

public class Objectandbyte {public static byte[] Tobytearray (Object obj) {byte[] bytes = NULL;
            try {//object to ByteArray bytearrayoutputstream bo = new Bytearrayoutputstream ();
            ObjectOutputStream oo = new ObjectOutputStream (bo);

            Oo.writeobject (obj);

            bytes = Bo.tobytearray ();
            Bo.close ();
        Oo.close ();
            catch (Exception e) {System.out.println ("translation" + E.getmessage ());
        E.printstacktrace ();
    return bytes; /** * Array to Object *@paramBytes *@return */public static Object toobject (byte[] bytes) {Object obj = null;
            try {Bytearrayinputstream bis = new Bytearrayinputstream (bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = Ois.readobject ();
            Ois.close ();
        Bis.close ();
        catch (IOException ex) {ex.printstacktrace ();
        catch (ClassNotFoundException ex) {ex.printstacktrace ();
    return obj; }
}

3, test



Related Article

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.