Redis Storage object and list<object>

Source: Internet
Author: User

Redis storage supports a type that does not have an object, although it supports list, but only supports list<string>

There are two ways to implement storage objects and generics

1. Serialization and deserialization

2.json


Serialization tool class to implement serialization and deserialization of Session objects and list collections

Package Com;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import java.io.Closeable; Import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.util.arraylist;import java.util.list;/** * Serialization Tool class * @author Caspar * */public class Serializeutil {/** * serialization * @param object * @return */public Static byte[] Serialize (Object object) {if (object = = null) {return null;} ObjectOutputStream oos = null; Bytearrayoutputstream baos = null;byte[] bytes = null;try {//serialization BAOs = new Bytearrayoutputstream (); oos = new ObjectOutput Stream (BAOs); Oos.writeobject (object); bytes = Baos.tobytearray ();} catch (Exception e) {e.printstacktrace ();} finally {close (Oos); close (BAOs);} return bytes;} /** * Deserialization * * @param bytes * @return */public static Object unserialize (byte[] bytes) {if (bytes = = null) {return null;}  Bytearrayinputstream Bais = Null;objectinputstream Ois = null;try {//deserialization Bais = new Bytearrayinputstream (bytes); ois = new ObjectInputStream (Bais); return Ois.readoBject ();} catch (Exception e) {e.printstacktrace ();} finally {close (Bais); close (OIS);} return null;} /** * Serialization List Collection * * @param list * @return */public static byte[] serializelist (list<?> list) {if (Commonutil.isem Ptylist (list)) {return null;} ObjectOutputStream oos = null; Bytearrayoutputstream baos = null;byte[] bytes = null;try {BAOs = new Bytearrayoutputstream (); oos = new ObjectOutputStream (BAOs); for (Object obj:list) {oos.writeobject (obj);} bytes = Baos.tobytearray ();} catch (Exception e) {e.printstacktrace ();} finally {close (Oos); close (BAOs);} return bytes;} /** * Deserialization List Collection * * @param lb * @return */public static list<?> unserializelist (byte[] bytes) {if (bytes = = NULL ) {return null;} list<object> list = new arraylist<object> ();  Bytearrayinputstream Bais = Null;objectinputstream Ois = null;try {//deserialization Bais = new Bytearrayinputstream (bytes); ois = new ObjectInputStream (Bais); while (bais.available () > 0) {Object obj = (object) ois.readobject (); if (obj = = null) {break;} List.add (obj);}} catch (Exception e) {e.printstacktrace ();} finally {close (Bais); close (OIS);} return list;} /** * Close IO Stream Object * * @param closeable */public static void Close (Closeable closeable) {if (closeable! = null) {try {closeabl E.close ();} catch (Exception e) {e.printstacktrace ();}}}}

Partial methods of the Redis tool class, implementing set/Get objects and generic values

 /** * Set Object * @param key * @param obj */public static void SetObject (String key, Object obj) {try { obj = obj = = null?    New Object (): obj; Getjedis (). Set (Key.getbytes (), serializeutil.serialize (obj));}        catch (Exception e) {e.printstacktrace ();}} /** * Get Object * @param key * @return Object */public static object GetObject (String key) {if (Getjedis () = = nul L | | !getjedis (). Exists (key)) {return null;} byte[] data = Getjedis (). Get (Key.getbytes ()); return (Object) serializeutil.unserialize (data);} /** * Set List collection * @param key * @param list */public static void setlist (String key,list<?> list) {try {if (commonutil.isnotemptylist (list)) {Getjedis (). Set (Key.getbytes (), Serializeutil.serializelist (list)    );    }else{//If the list is empty, set an empty Getjedis (). Set (Key.getbytes (), "". GetBytes ());    }} catch (Exception e) {e.printstacktrace ();}} /** * Get List Collection * @param key * @return */public static list<?>GetList (String key) {if (Getjedis () = null | |!getjedis (). Exists (key)) {return null;} byte[] data = Getjedis (). Get (Key.getbytes ()); return serializeutil.unserializelist (data);}


Test the Main method

public static void Main (string[] args) {    //object    setobject ("+", New Person ("Caspar", +));        Person P = (person) getObject ("+");    System.out.println (P.getname () + "----" +p.getage ());        List    list<person> list = new arraylist<person> ();    List.add (New person ("Tangmaru");    List.add (New person ("stool bear",));    List.add (New Person ("Little Lori", +));        Setlist ("list001", list);    List<person> resultlist = (list<person>) getList ("list001");    for (person person:resultlist) {System.out.println (Person.getname () + "----" +person.getage ());}}

Output results

Caspar----25
Tangmaru----39
Bear----Stool 33
Little Lori----14


Under normal circumstances, the efficiency is also very high, but if the high concurrency, serialization and deserialization consumption too much, Redis does not support the storage of object and generics, there is a reason.

We recommend using JSON to store

Convert the object and list<?> into JSON string format and set it into Redis, then get the JSON into the desired object, so it is simple and quick, it is recommended to use





Redis Storage object and list<object>

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.