Java redis 中写入对象

java  redis 存储对象 

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

public   class  Test {

       /**
      * jiachengwin
      *  @param  args
      */
       public   static   void  main(String[] args) {
           
             // 操作单独的文本串
           Jedis redis=  new  Jedis(  "localhost" , 6379);
           
           redis.set(  "key" "value" );
           System.  out .println(redis.get(  "key" ));
           System.  out .println(redis.del(  "key" ));
           
             // 操作对象
           Goods good=   new  Goods();  
           good.setName(   "天朝"  );
           good.setNum(1000);
           good.setPrice(19l);
           redis.set(  "yes" .getBytes(), SerializeUtil. serialize(good));
             byte [] value = redis.get(  "yes" .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(  "yes" .getBytes()));
           
       
           String key=  "yes-key" ;
           Goods g=   new  Goods();
           g.setName(   "天国"  );
           g.setNum(1200);
           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 jiachengwin
 *
 */
public   class  RedisClinet {
       private   static   final  String  ip =  "localhost" ;
       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;
     }

}






序列化工具类

/**
 *
 */
package redis.clients;

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

/**
 * @author jiachengwin
 *
 */
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;
     }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值