java的io设计
/** 基于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如下图所示