Redis存取List对象的实践

来源:互联网 发布:神仙道心动网络 编辑:程序博客网 时间:2024/06/06 01:01
package redis;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.alibaba.fastjson.JSON;import com.lz.art.pojo.Article;import com.lz.art.util.SerializeUtil;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisShardInfo;/************************************************** * uri方式配置的jedis  测试 **************************************************/@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath*:spring-config.xml"}) public class ReidsUriTest extends AbstractJUnit4SpringContextTests  {    @Autowired    JedisShardInfo jedisShardInfo;    //@Test    public void testJedisShardInfoSet(){        Jedis jedis = new Jedis(jedisShardInfo);        String result = jedis.set("girl", "BaBy8");        System.out.println("Set方法结果:" + result);    }    @Test    public void testSaveObject(){        /*******单机测试**********         * 【序列化List对象,整存整取】         * redis写10万条数据耗时:280            全部获取:100000            redis取10万条数据耗时:202            redis写100万条数据耗时:4142            全部获取:1000000            redis取10万条数据耗时:1486         */        Jedis jedis = new Jedis(jedisShardInfo);        List<Article> artList = new ArrayList<Article>();        for(int i=0;i<1000000;i++){            Article a = new Article();            a.setId("id-" + i);            a.setTitle("title-" + i);            artList.add(a);        }        long start = System.currentTimeMillis();        String key = "TestSetOpt";          jedis.set(key.getBytes(), SerializeUtil.serialize(artList));          long stored = System.currentTimeMillis();        System.out.println("redis写10万条数据耗时:" + (stored - start));        //验证          byte[] in = jedis.get(key.getBytes());          List<Article> list = SerializeUtil.unserializeForList(in);  //        for(Article obj : list){  //           System.out.println("测试Set操作 article title 是:" + obj.getTitle());  //        }          long end = System.currentTimeMillis();        System.out.println("全部获取:" + list.size());        System.out.println("redis取10万条数据耗时:" + (end - stored));    }    //@Test    public void testSaveList(){        /**** 单机测试  ************        redis存取10万条数据测试        redis写10万条数据耗时:35727        全部获取:100000        redis取10万条数据耗时:251        redis存取100万条数据测试        redis写10万条数据耗时:316339        全部获取:1000000        redis取10万条数据耗时:2243        *************************/        Jedis jedis = new Jedis(jedisShardInfo);        List<Article> artList = new ArrayList<Article>();        System.out.println("redis存取10万条数据测试");        long start = System.currentTimeMillis();        for(int i=0;i<100000;i++){            Article a = new Article();            a.setId("id-" + i);            a.setTitle("title-" + i);            String objData = JSON.toJSONString(a);            jedis.rpush("article", objData);            //System.out.println("保存对象数据:" + objData);        }        long stored = System.currentTimeMillis();        System.out.println("redis写10万条数据耗时:" + (stored - start));        List<String> list = jedis.lrange("article", 0, -1);        for(String str:list){            //System.out.println("[获取对象数据]:" + str);            Article art = JSON.parseObject(str,Article.class);            //System.out.println("标题:" + art.getTitle());        }        long end = System.currentTimeMillis();        System.out.println("全部获取:" + list.size());        System.out.println("redis取10万条数据耗时:" + (end - stored));    }}

SerializeUtil.java

package com.lz.art.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 序列化对象工具类,用于保存和读取redis数据使用 * Function: * Date: 2017年5月16日 */public class SerializeUtil  {    private static Logger log = LoggerFactory.getLogger(SerializeUtil.class);      /**     * 序列化对象     * @param object     * @return     */    public static byte[] serialize(Object object) {          ObjectOutputStream oos = null;          ByteArrayOutputStream baos = null;          byte[] bytes = null;        try {              // 序列化              baos = new ByteArrayOutputStream();              oos = new ObjectOutputStream(baos);              oos.writeObject(object);              bytes = baos.toByteArray();          } catch (Exception e) {              e.printStackTrace();          } finally {              try {                  if (oos != null) {                      oos.close();                  }                  if (baos != null) {                      baos.close();                  }              } catch (Exception e2) {                  e2.printStackTrace();              }          }          return bytes;      }      /**     * 反序列化对象     * @param bytes     * @return     */    public static Object unserialize(byte[] bytes) {         Object obj = null;         ByteArrayInputStream bais = null;          try {              // 反序列化              bais = new ByteArrayInputStream(bytes);              ObjectInputStream ois = new ObjectInputStream(bais);              obj = ois.readObject();              ois.close();               bais.close();        } catch (Exception e) {              e.printStackTrace();          }          return obj;      }      /**     * 关闭的数据源或目标。调用 close()方法可释放对象保存的资源(如打开文件)     * 关闭此流并释放与此流关联的所有系统资源。如果已经关闭该流,则调用此方法无效。     * @param closeable     */    public static void close(Closeable closeable) {          if (closeable != null) {              try {                  closeable.close();              } catch (Exception e) {                  log.info("Unable to close %s", closeable, e);              }          }      }    /**     * 列表序列化(用于Redis整存整取)     * @param value     * @return     */    public static <T> byte[] serialize(List<T> value) {          if (value == null) {              throw new NullPointerException("Can't serialize null");          }          byte[] rv=null;          ByteArrayOutputStream bos = null;          ObjectOutputStream os = null;          try {              bos = new ByteArrayOutputStream();              os = new ObjectOutputStream(bos);              for(T obj : value){                  os.writeObject(obj);              }              os.writeObject(null);              os.close();              bos.close();              rv = bos.toByteArray();          } catch (IOException e) {              throw new IllegalArgumentException("Non-serializable object", e);          } finally {              close(os);            close(bos);        }          return rv;      }    /**     * 反序列化列表(用于Redis整存整取)     * @param in     * @return     */    public static <T> List<T> unserializeForList(byte[] in) {          List<T> list = new ArrayList<T>();          ByteArrayInputStream bis = null;          ObjectInputStream is = null;          try {              if(in != null) {                  bis=new ByteArrayInputStream(in);                  is=new ObjectInputStream(bis);                  while (true) {                      T obj = (T) is.readObject();                      if(obj == null){                          break;                      }else{                          list.add(obj);                      }                  }                  is.close();                  bis.close();              }          } catch (IOException e) {              log.warn("Caught IOException decoding %d bytes of data",                      in == null ? 0 : in.length, e);          } catch (ClassNotFoundException e) {              log.warn("Caught CNFE decoding %d bytes of data",                      in == null ? 0 : in.length, e);          } finally {              close(is);              close(bis);          }          return list;      }  }