现在NoSQL已经火的不能再火了,听了这么长时间,今天才试了一下。
java驱动用的是jedis 2.0.0,可以到这下:http://mvnrepository.com/artifact/redis.clients/jedis
刚开始写了个入门级程序插入redis,每秒才900条左右,不仅让我大喊坑爹,但是一想,
额····是单线程····
起了10个线程后,到每秒到9000条了。
现贴出代码:
package com.test;
import redis.clients.jedis.*;
public class RedisTest {
private static JedisPool pool;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxIdle(20);
config.setMaxWait(1000);
config.setTestOnBorrow(true);
pool = new JedisPool(config, "10.10.67.9");
}
public static void main(String[] args) {
RedisTest demo = new RedisTest();
demo.test();
}
public void test() {
testThread();
// initInsert();
// flush();
// getData(1000000);
}
private void initInsert() {
System.out.println("RedisTest 插入数据开始");
Jedis jedis = (Jedis) pool.getResource();
for (long i = 0; i <= 1000000; i++) {
jedis.set(EhcacheTest.long2Ip(i), "cmcc");
}
pool.returnResource(jedis);
System.out.println("RedisTest 插入数据结束");
}
private void getData(long key) {
Jedis jedis = (Jedis) pool.getResource();
long time = System.currentTimeMillis();
System.out.println(jedis.get(EhcacheTest.long2Ip(key)));
System.out.println(System.currentTimeMillis() - time);
System.out.println(jedis.dbSize());
}
private void flush() {
Jedis jedis = (Jedis) pool.getResource();
jedis.flushAll();
}
private void testThread() {
Thread thread[] = new Thread[10];
for (int i = 0; i < thread.length; i++) {
thread[i] = new MyThread();
thread[i].setName(String.valueOf(i));
thread[i].start();
}
}
class MyThread extends Thread {
@Override
public void run() {
int num = Integer.parseInt(this.getName()) * 100000;
Jedis jedis = RedisTest.pool.getResource();
for (int i = num; i < num + 100000; i++) {
jedis.set(EhcacheTest.long2Ip(i), "cmcc");
}
}
}
}
上面的例子借鉴了此处:http://blog.csdn.net/zuoanlove/article/details/6595229