/users/sherry/workpath/git/web/redisdemo/src/main/java/org/zln/utils/jedisutils.java
Packageorg.zln.utils;Importorg.apache.commons.lang3.StringUtils;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;/*** Created by Sherry on 16/9/13.*/ Public classJedisutils {Private StaticLogger Logger =Logmanager.getlogger (); Private StaticJedispool pool =NULL; Private Static FinalString URL = "127.0.0.1"; Private Static Final intPORT = 6379; Private StaticJedis Jedis =NULL; Static{Pool=NewJedispool (Newjedispoolconfig (), url,port); } Public StaticJedis Getrediscon (String password) {Jedis=Pool.getresource (); if(stringutils.isnotempty (password)) {Logger.info ("With Password:" +password+ "Landing Redis"); Jedis.auth (password); } logger.info (Jedis.ping ()); returnJedis; } Public Static voidCloserediscon (Jedis Jedis) {if(jedis!=NULL) {jedis.close (); } } Public Static voidCloseapp () {//Execute when closing the applicationPool.destroy (); }}
/users/sherry/workpath/git/web/redisdemo/src/main/java/org/zln/main/jedismain.java
PackageOrg.zln.main;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;Importorg.zln.utils.JedisUtils;ImportRedis.clients.jedis.Jedis;ImportJava.io.*;ImportJava.util.*;/*** Created by Sherry on 16/9/13.*/ Public classJedismain {Private StaticLogger Logger =Logmanager.getlogger (); Public Static voidMain (string[] args) {Jedis Jedis= Jedisutils.getrediscon ("");//stringtest (Jedis);////listtest (Jedis);////maptest (Jedis);////objtest (Jedis);objlisttest (Jedis);//return this Jedis instance to Jedispool. Jedisutils.closerediscon (Jedis); } /*** Object list *@paramJedis*/ Private Static voidobjlisttest (Jedis jedis) {person Person1=NewPerson ("name1", 1); Person Person2=NewPerson ("name2", 2); Person Person3=NewPerson ("Name3", 3); Person Person4=NewPerson ("Name4", 4); List<Person> persons =NewArraylist<>(); Persons.add (Person1); Persons.add (Person2); Persons.add (Person3); Persons.add (PERSON4); Try { for(person person:persons) {Bytearrayoutputstream BOS=NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject (person); byte[] ByteArray =Bos.tobytearray (); Oos.close (); Bos.close (); Logger.info ("Write to object:" +Person ); Jedis.lpush ("Persons". GetBytes (), ByteArray); } List<byte[]> personsbytes = jedis.lrange ("Persons". GetBytes (), 0,10); for(byte[] bs:personsbytes) {Bytearrayinputstream bis=NewBytearrayinputstream (BS); ObjectInputStream InputStream=NewObjectInputStream (bis); Person ReadObject=(person) inputstream.readobject (); Logger.info ("Read object \ T" +readobject.tostring ()); Inputstream.close (); Bis.close (); } } Catch(ioexception|classnotfoundexception e) {E.printstacktrace (); } Jedis.del ("Persons". GetBytes ()); } /*** Storage Object *@paramJedis*/ Private Static voidobjtest (Jedis Jedis) { person person=NewPerson (); Person.setage (27); Person.setname ("Kaka card"); Bytearrayoutputstream Bos=NewBytearrayoutputstream (); Try{objectoutputstream Oos=NewObjectOutputStream (BOS); Oos.writeobject (person); byte[] ByteArray =Bos.tobytearray (); Oos.close (); Bos.close (); String Setobjectret=Jedis.set (Person.getname (). GetBytes (), ByteArray); Logger.info ("Set object return \ t" +Setobjectret); byte[] bs =Jedis.get (Person.getname (). GetBytes ()); Bytearrayinputstream bis=NewBytearrayinputstream (BS); ObjectInputStream InputStream=NewObjectInputStream (bis); Person ReadObject=(person) inputstream.readobject (); Logger.info ("Read object \ T" +readobject.tostring ()); Inputstream.close (); Bis.close (); } Catch(ioexception|classnotfoundexception e) {E.printstacktrace (); } Jedis.del (Person.getname ()); } /*** Store Map *@paramJedis*/ Private Static voidmaptest (Jedis jedis) {Map<String,String> user =NewHashmap<string,string>(); User.put ("Name", "CD" ); User.put ("Password", "123456" ); Jedis.hmset ("User", user); //Number of MapkeySystem.out.println (String.Format ("len:%d", Jedis.hlen ("User" )));//all the key values in the mapSystem.out.println (String.Format ("Keys:%s", Jedis.hkeys ("User" ) ));//all the value in the mapSystem.out.println (String.Format ("Values:%s", Jedis.hvals ("User" ) ));//Remove the Name field value from the maplist<string> Rsmap = jedis.hmget ("User", "name", "Password" ); System.out.println (RSMAP);//Delete One of the key values in the map passwordJedis.hdel ("User", "password" ); System.out.println (Jedis.hmget ("User", "name", "Password" )); Jedis.del ("User"); } /*** Storage list *@paramJedis*/ Private Static voidlisttest (Jedis Jedis) {Jedis.lpush ("Tutorial-list", "Redis"); Jedis.lpush ("Tutorial-list", "Mongodb"); Jedis.lpush ("Tutorial-list", "Mysql"); //get the stored data and outputlist<string> list = Jedis.lrange ("Tutorial-list", 0, 5); for(inti=0; I<list.size (); i++) {System.out.println ("Stored string in Redis::" +List.get (i)); } Jedis.del ("Tutorial-list"); } /*** Store String *@paramJedis*/ Private Static voidstringtest (Jedis Jedis) {//add again and againJedis.set ("string1", "one string"); Jedis.set ("String2", "number second string"); Jedis.set ("String3", "Number third string"); Jedis.set ("String4", "Number fourth string"); Logger.info ("Get string1:" +jedis.get ("string1")); //On the existing key, add the value to theJedis.append ("string1", "Add new string"); Logger.info ("Get string1:" +jedis.get ("string1")); //add multiple key-value pairs at onceJedis.mset ("S1", "v1", "S2", "V2", "S3", "V3"); Logger.info ("Get S1:" +jedis.get ("S1")); for(Iterator<string> Iterator = Jedis.keys ("*"). iterator (); Iterator.hasnext ();) {String key=Iterator.next (); Logger.info (Key+":"+Jedis.get (key)); Jedis.del (key); } }}
Java Operations Redis Advanced