redis直接存储集合

package com.lq.test;
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.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import redis.clients.jedis.Jedis;
  
  
public class JedisTest {  
    /** 
     * Test Data 
     * @return 
     */  
    public static List buildTestData(){  
        User a = new User();  
        a.setName("a");  
        User b = new User();  
        b.setName("b");  
        List list = new ArrayList();  
        list.add(a);  
        list.add(b);  
        return list;  
    }  
  
    /** 
     * Test for 
     */  
    public static void testSetElements(){  
        List testData = buildTestData();  
        Jedis jedis = new Jedis("localhost"); ;  
        String key = "testSetElements" + new Random(1000).nextInt();  
        jedis.set(key.getBytes(), ObjectsTranscoder.serialize(testData));  
        //验证  
        byte[] in = jedis.get(key.getBytes());  
        List list = ObjectsTranscoder.deserialize(in);  
        for(User user : list){  
            System.out.println("testSetElements user name is:" + user.getName());  
        }  
    }  
  
    public static void testSetEnsemble(){  
        List testData = buildTestData();  
        Jedis jedis = new Jedis("localhost");  
        String key = "testSetEnsemble" + new Random(1000).nextInt();  
        jedis.set(key.getBytes(), ListTranscoder.serialize(testData));  
  
        //验证  
        byte[] in = jedis.get(key.getBytes());  
        List list = (List)ListTranscoder.deserialize(in);  
        for(User user : list){  
            System.out.println("testSetEnsemble user name is:" + user.getName());  
        }  
    }  
  
    public static void main(String[] args) {  
        //testSetElements();  
        testSetEnsemble();  
    }  
  
    public static void close(Closeable closeable) {  
        if (closeable != null) {  
            try {  
                closeable.close();  
            } catch (Exception e) {  
                e.printStackTrace();
            }  
        }  
    }  
  
    static class User implements Serializable{  
        String name;  
  
        public String getName() {  
            return name;  
        }  
  
        public void setName(String name) {  
            this.name = name;  
        }  
    }  
    
    //对象转换工具类
    static class ObjectsTranscoder{  
        
    	//对象转字节数组
        public static byte[] serialize(List 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(User user : value){  
                    os.writeObject(user);  
                }  
                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;  
        }  
        //字节数组转对象
        public static List deserialize(byte[] in) {  
            List list = new ArrayList();  
            ByteArrayInputStream bis = null;  
            ObjectInputStream is = null;  
            try {  
                if(in != null) {  
                    bis=new ByteArrayInputStream(in);  
                    is=new ObjectInputStream(bis);  
                    while (true) {  
                        User user = (User) is.readObject();  
                        if(user == null){  
                            break;  
                        }else{  
                            list.add(user);  
                        }  
                    }  
                    is.close();  
                    bis.close();  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            }finally{
            	try {
					bis.close();
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }  
            return list;  
        }  
    }  
    
    //集合转换工具类
    static class ListTranscoder{  
        //集合转字节数组
    	public static byte[] serialize(Object 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);  
                os.writeObject(value);  
                os.close();  
                bos.close();  
                rv = bos.toByteArray();  
            } catch (IOException e) {  
                throw new IllegalArgumentException("Non-serializable object", e);  
            } finally {  
                close(os);  
                close(bos);  
            }  
            return rv;  
        }  
    	//字节数组转集合
        public static Object deserialize(byte[] in) {  
            Object rv=null;  
            ByteArrayInputStream bis = null;  
            ObjectInputStream is = null;  
            try {  
                if(in != null) {  
                    bis=new ByteArrayInputStream(in);  
                    is=new ObjectInputStream(bis);  
                    rv=is.readObject();  
                    is.close();  
                    bis.close();  
                }  
            } catch (Exception e) {  
            	e.printStackTrace();
            }finally{
            	try {
					bis.close();
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
            return rv;  
        }  
    }  
} 

你可能感兴趣的:(redis)