Redis 存储字符串和对象

今天用redis存储,发现客户端jedis提供的存储方法中存储的类型只有String和byte数据,没有能够存储对象的,网上发现可以序列化存储对象。这就开始了我第一次序列化之旅。



1 测试类

import redis.clients.RedisClinet;
import redis.clients.SerializeUtil;
import redis.clients.jedis.Jedis;

public class Test {

      /**
      * Administrator
      * @param args
      */
      public static void main(String[] args) {
           
            // 操作单独的文本串
           Jedis redis= new Jedis( "10.2.31.38", 6379);
           
           redis.set( "key""value");
           System. out.println(redis.get( "key"));
           System. out.println(redis.del( "key"));
           
            // 操作实体类对象
           Goods good= new Goods();  // 这个Goods实体我就不写了啊
           good.setName( "洗衣机" );
           good.setNum(400);
           good.setPrice(19l);
           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()));
           
            // 操作实体类对象2(实际上和上面是一样的)
           String key= "goods-key";
           Goods g= new Goods();
           g.setName( "电风扇--d" );
           g.setNum(200);
           String temp=RedisClinet. getInstance().set(g, key);
           System. out.println(temp);
           
           Object o=RedisClinet. getInstance().get(key);
            if(o!= null)
           {
                Goods g1=(Goods)o;
                System. out.println(g1.getName());
                System. out.println(g1.getNum());
           }
          System. out.println(RedisClinet. getInstance().del(key));
           
     }
}


RedisClinet 客户端类
package redis.clients;
import redis.clients.jedis.Jedis;
/**
 *
 * @author ajun
 *
 */
public class RedisClinet {
      private static final String ip= "10.2.31.38";
      private static final int port=6379;
      protected static RedisClinet redis new RedisClinet ();
      protected static Jedis jedis = new Jedis( ip, port);;
      static {
           
     }
    protected RedisClinet(){
       System. out.println( " init Redis ");
    }
    public static RedisClinet getInstance()
    {
        return redis;
    }
   
    /**set Object*/
      public String set(Object object,String key)
     {
            return jedis.set(key.getBytes(), SerializeUtil.serialize(object));
     }
     
      /**get Object*/
      public Object get(String key)
     {
            byte[] value = jedis.get(key.getBytes());
            return SerializeUtil. unserialize(value);
     }
     
      /**delete a key**/
      public boolean del(String key)
     {
            return jedis.del(key.getBytes())>0;
     }

}


3 序列化工具类

/**
 *
 */
package redis.clients;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Administrator
 *
 */
public class SerializeUtil {
      public static byte[] serialize(Object object) {
           ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                 // 序列化
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                 byte[] bytes = baos.toByteArray();
                 return bytes;
           } catch (Exception e) {

           }
            return null;
     }

      public static Object unserialize( byte[] bytes) {
           ByteArrayInputStream bais = null;
            try {
                 // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                 return ois.readObject();
           } catch (Exception e) {

           }
            return null;
     }
}

阅读更多
换一批

没有更多推荐了,返回首页