序列化与io

java的io设计

 

140650_86YY_3268705.png

/** 基于Dubbo的分布式系统架构视频教程,吴水成,wu-sc@foxmail.com,学习交流QQ群:367211134 **/
package wusc.edu.demo.redis;

import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 
 * @描述: Redis测试 .
 * @作者: WuShuicheng .
 * @创建时间: 2015-3-23,上午1:30:40 .
 * @版本号: V1.0 .
 */
public class RedisSpringTest {
	private static final Log log = LogFactory.getLog(RedisSpringTest.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
			
			ShardedJedisPool pool = (ShardedJedisPool) context.getBean("shardedJedisPool");
			ShardedJedis jedis = pool.getResource();
			
			
			String key = "zq";
			String value = "";
			
			log.info(key + "=" + jedis.get(key));
			
			jedis.del(key); // 删数据
			
			log.info("delete");
			log.info(key + "=" + jedis.get(key));
			
			jedis.set(key, "ZengQi"); // 存数据
			value = jedis.get(key); // 取数据
			log.info("set");
			log.info(key + "=" + value);
			
			jedis.set(key, "WuShuicheng"); // 存数据
			value = jedis.get(key); // 取数据
			log.info("set overwrite");
			log.info(key + "=" + value);
			
			jedis.del(key); // 删数据
			value = jedis.get(key); // 取数据
			log.info("del");
			log.info(key + "=" + value);
			
			

	        User user = new User();
	        user.setId("123124");
	        user.setName("fighter");

	        // 存入一个 user对象
	        jedis.set("user".getBytes(), SerializationUtils.serialize(user));

	        // 获取
	        byte[] bs = jedis.get("user".getBytes());
	        User desUser = (User) SerializationUtils.deserialize(bs);
	        System.out.println(desUser.getId() + ":" + desUser.getName());

			context.stop();
		} catch (Exception e) {
			log.error("==>RedisSpringTest context start error:", e);
			System.exit(0);
		} finally {
			log.info("===>System.exit");
			System.exit(0);
		}
	}
}

 

上图中利用org.apache.commons.lang3.SerializationUtils对序列化对象User进行了一次反序列化,存redis中存的byte[]数据对象,redis中存储的user如下图所示

141559_fGrU_3268705.png

 

 

 

转载于:https://my.oschina.net/u/3268705/blog/847705

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值