redis 简单测试
package com.yhxia.cache;import java.util.HashMap;import java.util.Map;import org.junit.Test;import redis.clients.jedis.Jedis;import com.yhxia.cache.common.People;import com.yhxia.cache.common.SerializeUtil;public class RedisTest { private final int total = 10000000;// 千万 @Test public void saveMapTest() { Jedis jedis = new Jedis("127.0.0.1", 6379); Map<Integer,People> map = new HashMap<Integer,People>(); for (int i = 0; i <= total; i++) { People p = new People(); p.setId(i); p.setName("name_" + i); map.put(i, p); } byte[] sp = SerializeUtil.serialize((HashMap<Integer,People>)map); jedis.set("peoples".getBytes(), sp); } @Test public void saveObjTest() { Jedis jedis = new Jedis("127.0.0.1", 6379); for (int i = 0; i <= total; i++) { People p = new People(); p.setId(i); p.setName("name_" + i); byte[] sp = SerializeUtil.serialize(p); jedis.set((i + "").getBytes(), sp); } } @Test public void getTest() { Jedis jedis = new Jedis("127.0.0.1", 6379); int count = 0; for (int i = 0; i <= total; i++) { People p = (People) SerializeUtil.unserialize(jedis.get((i + "") .getBytes())); if (p != null) count++; } System.out.println("object count:" + count); }}
?
package com.yhxia.cache.common;import java.io.Serializable;public class People implements Serializable{ private static final long serialVersionUID = 1L; private String name = ""; private int id = 0; public People(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
?
对redis 的配置和启动,不是重点,不做叙述,可以参考:
redis:http://www.cnblogs.com/mejozn/archive/2013/05/05/3061949.html
jedis(java 调用库):http://www.blogways.net/blog/2013/06/02/jedis-demo.html
(其他扩展:https://github.com/xetorthio/jedis?http://snowolf.iteye.com/blog/1633196)
?