Java 操作 Redis 高级
/Users/sherry/WorkPath/Git/Web/redisDemo/src/main/java/org/zln/utils/JedisUtils.java
package org.zln.utils;import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;/**
Created by sherry on 16/9/13.
*/
public class JedisUtils {private static Logger logger = LogManager.getLogger();
private static JedisPool pool = null;private static final String URL = "127.0.0.1";
private static final int PORT = 6379;private static Jedis jedis = null;
static {
pool = new JedisPool(new JedisPoolConfig(), URL,PORT);}
public static Jedis getRedisCon(String password){
jedis = pool.getResource();
if (StringUtils.isNotEmpty(password)){
logger.info("以密码:"+password+"登陆 redis");
jedis.auth(password);
}
logger.info(jedis.ping());
return jedis;
}public static void closeRedisCon(Jedis jedis){
if (jedis!=null){
jedis.close();
}
}public static void closeApp(){
//关闭应用程序的时候执行
pool.destroy();
}}
/Users/sherry/WorkPath/Git/Web/redisDemo/src/main/java/org/zln/main/JedisMain.java
package org.zln.main;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.zln.utils.JedisUtils;
import redis.clients.jedis.Jedis;import java.io.;
import java.util.;/**
objListTest(jedis); // 将这个 Jedis 实例归还给 JedisPool。 JedisUtils.closeRedisCon(jedis);
Created by sherry on 16/9/13.
*/
public class JedisMain {private static Logger logger = LogManager.getLogger();
public static void main(String[] args) {
Jedis jedis = JedisUtils.getRedisCon("");
// stringTest(jedis);
//
// listTest(jedis);
//
// mapTest(jedis);
//
// objTest(jedis);} </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 对象列表 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> objListTest(Jedis jedis) { Person person1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Person("name1",1<span style="color: rgba(0, 0, 0, 1)">); Person person2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Person("name2",2<span style="color: rgba(0, 0, 0, 1)">); Person person3 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Person("name3",3<span style="color: rgba(0, 0, 0, 1)">); Person person4 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Person("name4",4<span style="color: rgba(0, 0, 0, 1)">); List</span><Person> persons = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">(); persons.add(person1); persons.add(person2); persons.add(person3); persons.add(person4); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Person person:persons){ ByteArrayOutputStream bos </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ByteArrayOutputStream(); ObjectOutputStream oos </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectOutputStream(bos); oos.writeObject(person); </span><span style="color: rgba(0, 0, 255, 1)">byte</span> [] byteArray =<span style="color: rgba(0, 0, 0, 1)"> bos.toByteArray(); oos.close(); bos.close(); logger.info(</span>"写入对象:"+<span style="color: rgba(0, 0, 0, 1)">person); jedis.lpush(</span>"persons"<span style="color: rgba(0, 0, 0, 1)">.getBytes(),byteArray); } List</span><<span style="color: rgba(0, 0, 255, 1)">byte</span>[]> personsBytes = jedis.lrange("persons".getBytes(),0,10<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">byte</span><span style="color: rgba(0, 0, 0, 1)">[] bs:personsBytes){ ByteArrayInputStream bis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ByteArrayInputStream(bs); ObjectInputStream inputStream </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectInputStream(bis); Person readObject </span>=<span style="color: rgba(0, 0, 0, 1)"> (Person) inputStream.readObject(); logger.info( </span>" 读取对象 \t" +<span style="color: rgba(0, 0, 0, 1)"> readObject.toString()); inputStream.close(); bis.close(); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (IOException|<span style="color: rgba(0, 0, 0, 1)">ClassNotFoundException e) { e.printStackTrace(); } jedis.del(</span>"persons"<span style="color: rgba(0, 0, 0, 1)">.getBytes()); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 存储对象 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> objTest(Jedis jedis) { Person person </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Person(); person.setAge(</span>27<span style="color: rgba(0, 0, 0, 1)">); person.setName(</span>"卡卡卡"<span style="color: rgba(0, 0, 0, 1)">); ByteArrayOutputStream bos </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ByteArrayOutputStream(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { ObjectOutputStream oos </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectOutputStream(bos); oos.writeObject(person); </span><span style="color: rgba(0, 0, 255, 1)">byte</span> [] byteArray =<span style="color: rgba(0, 0, 0, 1)"> bos.toByteArray(); oos.close(); bos.close(); String setObjectRet </span>=<span style="color: rgba(0, 0, 0, 1)"> jedis.set(person.getName().getBytes(), byteArray); logger.info( </span>" set object return \t" +<span style="color: rgba(0, 0, 0, 1)"> setObjectRet); </span><span style="color: rgba(0, 0, 255, 1)">byte</span> [] bs =<span style="color: rgba(0, 0, 0, 1)"> jedis.get( person.getName().getBytes()); ByteArrayInputStream bis </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ByteArrayInputStream(bs); ObjectInputStream inputStream </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectInputStream(bis); Person readObject </span>=<span style="color: rgba(0, 0, 0, 1)"> (Person) inputStream.readObject(); logger.info( </span>" read object \t" +<span style="color: rgba(0, 0, 0, 1)"> readObject.toString()); inputStream.close(); bis.close(); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (IOException|<span style="color: rgba(0, 0, 0, 1)">ClassNotFoundException e) { e.printStackTrace(); } jedis.del(person.getName()); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 存储 Map * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> mapTest(Jedis jedis) { Map</span><String,String> user = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<String,String><span style="color: rgba(0, 0, 0, 1)">(); user.put( </span>"name" , "cd"<span style="color: rgba(0, 0, 0, 1)"> ); user.put( </span>"password" , "123456"<span style="color: rgba(0, 0, 0, 1)"> ); jedis.hmset(</span>"user"<span style="color: rgba(0, 0, 0, 1)">,user); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> mapkey 个数</span> System.out.println(String.format ( "len:%d" , jedis.hlen( "user"<span style="color: rgba(0, 0, 0, 1)"> )));
//map 中的所有键值
System.out.println(String.format ( "keys: %s" , jedis.hkeys( "user" )));
//map 中的所有 value
System.out.println(String.format ( "values: %s" , jedis.hvals( "user" )));
//取出 map 中的 name 字段值
List<String> rsmap = jedis.hmget( "user" , "name" , "password" );
System.out.println(rsmap);
//删除 map 中的某一个键值 password
jedis.hdel( "user" , "password" );
System.out.println(jedis.hmget( "user" , "name" , "password" ));jedis.del(</span>"user"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 存储列表 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> listTest(Jedis jedis) { jedis.lpush(</span>"tutorial-list", "Redis"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"tutorial-list", "Mongodb"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"tutorial-list", "Mysql"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取存储的数据并输出</span> List<String> list = jedis.lrange("tutorial-list", 0 ,5<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i=0; i<list.size(); i++<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(</span>"Stored string in redis:: "+<span style="color: rgba(0, 0, 0, 1)">list.get(i)); } jedis.del(</span>"tutorial-list"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 存储字符串 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> jedis </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> stringTest(Jedis jedis) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一次次添加</span> jedis.set("string1","一号字符串"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"string2","二号字符串"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"string3","三号字符串"<span style="color: rgba(0, 0, 0, 1)">); jedis.set(</span>"string4","四号字符串"<span style="color: rgba(0, 0, 0, 1)">); logger.info(</span>"获取string1:"+jedis.get("string1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在已有 key 上,对 value 进行新增</span> jedis.append("string1","添加新字符串"<span style="color: rgba(0, 0, 0, 1)">); logger.info(</span>"获取string1:"+jedis.get("string1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一次性添加多个键值对</span> jedis.mset("s1","v1","s2","v2","s3","v3"<span style="color: rgba(0, 0, 0, 1)">); logger.info(</span>"获取s1:"+jedis.get("s1"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (Iterator<String> iterator = jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">).iterator();iterator.hasNext();){ String key </span>=<span style="color: rgba(0, 0, 0, 1)"> iterator.next(); logger.info(key</span>+":"+<span style="color: rgba(0, 0, 0, 1)">jedis.get(key)); jedis.del(key); } }
}