Redis does not provide a method for set (String key, Object obj), but it provides a method for set (final byte[] key, final byte[] value), which can be stored by converting the object into a byte array.
Object class
PackageRedis;Importjava.io.Serializable; Public classGoodsImplementsSerializable {/** * */ Private Static Final LongSerialversionuid = 3870927632803751641L; PrivateString name; Private intnum; Private intPrice ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetnum () {returnnum; } Public voidSetnum (intnum) { This. num =num; } Public intGetPrice () {returnPrice ; } Public voidSetprice (intPrice ) { This. Price =Price ; }}
Serialization Tool class
PackageRedis;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;/** * @authorAdministrator **/ Public classSerializeutil { Public Static byte[] Serialize (Object object) {ObjectOutputStream Oos=NULL; Bytearrayoutputstream BAOs=NULL; Try { //Serialization ofBAOs =NewBytearrayoutputstream (); Oos=NewObjectOutputStream (BAOs); Oos.writeobject (object); byte[] bytes =Baos.tobytearray (); returnbytes; } Catch(Exception e) {}return NULL; } Public StaticObject Unserialize (byte[] bytes) {Bytearrayinputstream Bais=NULL; Try { //deserializationBais =Newbytearrayinputstream (bytes); ObjectInputStream Ois=NewObjectInputStream (Bais); returnOis.readobject (); } Catch(Exception e) {}return NULL; }}
Redis Save Object Extension class
PackageRedis;ImportRedis.clients.jedis.Jedis;/** * * @authorAjun **/ Public classredisclient {Private Static FinalString IP = "192.168.77.135"; Private Static Final intPort = 6379; protected StaticRedisclient Redis =Newredisclient (); protected StaticJedis Jedis =NewJedis (IP, port);; Static { } protectedredisclient () {System.out.println ("Init Redis"); } Public Staticredisclient getinstance () {returnRedis; } /**Set Object*/ Publicstring Set (Object object, String key) {returnJedis.set (Key.getbytes (), Serializeutil.serialize (object)); } /**Get Object*/ PublicObject get (String key) {byte[] Value =Jedis.get (Key.getbytes ()); returnserializeutil.unserialize (value); } /**Delete a key **/ Public Booleandel (String key) {returnJedis.del (Key.getbytes ()) > 0; }}
Test class
PackageRedis;ImportRedis.clients.jedis.Jedis; Public classSaveobjecttest {/*** Administrator * *@paramargs*/ Public Static voidMain (string[] args) {//manipulating a separate text stringJedis Redis =NewJedis ("192.168.77.135", 6379); Redis.set ("Key", "value"); System.out.println (Redis.get ("Key")); System.out.println (Redis.del ("Key")); //manipulating entity class objectsGoods good =NewGoods ();//I'm not going to write this goods entity .Good.setname ("Washing Machine"); Good.setnum (400); Good.setprice (19); Redis.set ("Good". GetBytes (), serializeutil.serialize (good)); byte[] value = Redis.get ("Good". GetBytes ()); Object Object=serializeutil.unserialize (value); if(Object! =NULL) {Goods Goods=(Goods) object; System.out.println (Goods.getname ()); System.out.println (Goods.getnum ()); System.out.println (Goods.getprice ()); } System.out.println (Redis.del ("Good". GetBytes ())); //manipulating Entity Class object 2 (actually the same as above)String key = "Goods-key"; Goods g=NewGoods (); G.setname ("Fan--d"); G.setnum (200); String Temp=redisclient.getinstance (). Set (g, key); SYSTEM.OUT.PRINTLN (temp); Object o=redisclient.getinstance (). get (key); if(O! =NULL) {Goods G1=(Goods) o; System.out.println (G1.getname ()); System.out.println (G1.getnum ()); } System.out.println (Redisclient.getinstance (). Del (key)); }}
Java uses redis2--to save objects