直接上源码 有不妥之处 望牛人指教!。。。 要用到jedis 需要引入两个jar包 可在我的资源中直接下载: commons-pool2-2.3.jar jedis-2.4.1.jar
Redis工具类
package com.test.redis;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
publicclass JedisUtils {
privatestaticLogger log= LoggerFactory.getLogger(JedisUtils.class);
privatestaticJedisPool pool= null;
/**
* 获得连接池
* @param ip
* @paramport
* @return JedisPool
*/
publicstatic JedisPoolgetPool(String ip,intport){
if(null == pool){
JedisPoolConfig config = new JedisPoolConfig();
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxTotal个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(500);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(5);
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(20000);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
pool = new JedisPool(config, ip, port,1500000);
log.info("access ip: {1} port: {2} connect.",newString[]{ip,String.valueOf(port)});
}
returnpool;
}
/**
* 返还到连接池
* @param pool
* @param redis
*/
publicstaticvoid returnSource(JedisPool pool,Jedis redis){
if(null != redis){
pool.returnResource(redis);
}
}
/**
* 获得数据
* @param key
* @return
*/
publicstatic StringgetValue(String key){
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try{
pool = getPool("127.0.0.1",6379);
jedis = pool.getResource();
value = jedis.get(key);
}catch(Exception e){
pool.returnBrokenResource(jedis);//释放jedis对象
e.getMessage();
}finally{
returnSource(pool,jedis);//返还到连接池
}
return value;
}
/**
* 字符串转换为日期
* @param dateStr
* @return
* @author
*/
publicstatic DateStringToDate(String dateStr){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try{
if(!"".equals(dateStr)&&null!= dateStr){
date = format.parse(dateStr);
}
}catch(Exception e){
log.info("format dateStr error "+ e.getMessage());
returnnull;
}
return date;
}
/**
* 日期转换为字符串
* @param dateStr
* @return
*/
publicstatic StringdateToString(Date date){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = "";
try{
if(null != date){
dateStr = format.format(date);
}
}catch(Exception e){
log.info("format date error "+ e.getMessage());
returnnull;
}
return dateStr;
}
}
Redis插入、查询数据测试类
package com.test.redis;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisTest {
publicstatic JedisPool pool = null;
publicstatic Jedis jedis = null;
publicstatic void main(String[] args) {
pool= JedisUtils.getPool("127.0.0.1", 6379);
jedis= pool.getResource();
insertData(jedis);
selectData(jedis);
}
/**
* 插入5000000条数据的时间计算
*
* @param jedis
*/
publicstatic void insertData(Jedis jedis) {
longstart = System.currentTimeMillis();
System.out.println(System.currentTimeMillis());
for(int i = 0; i < 5000000; i++) {
Map hashMap = new HashMap();
hashMap.put("vehicled","" + i);
hashMap.put("plateNo","No" + i);
hashMap.put("simNo","No" + i);
hashMap.put("location","测试测试");
hashMap.put("sendTime",JedisUtils.dateToString(new Date()));
hashMap.put("velocity","" + i);
hashMap.put("longitude","" + i);
hashMap.put("latitude","" + i);
hashMap.put("status","" + i);
hashMap.put("alarmState","" + i);
jedis.hmset("gpsInfo"+ i, hashMap);
}
System.out.println(System.currentTimeMillis());
longend = System.currentTimeMillis();
System.out.println(end- start);
}
/**
* 查询1000000 条数据的时间计算
*
* @param jedis
*/
publicstatic void selectData(Jedis jedis) {
longstart = System.currentTimeMillis();
System.out.println(System.currentTimeMillis());
for(int i = 0; i < 1000000) {
Map map = jedis.hgetAll("gpsInfo" + i);
Stringvehicled = map.get("vehicled");
StringplateNo = map.get("plateNo");
StringsimNo = map.get("simNo");
Stringlocation = map.get("location");
StringsendTime = map.get("sendTime");
Stringvelocity = map.get("velocity");
Stringlongitude = map.get("longitude");
Stringlatitude = map.get("latitude");
Stringstatus = map.get("status");
StringalarmState = map.get("alarmState");
System.out.println("vehicled= " + vehicled + "," + "plateNo = "
+plateNo + "," + "simNo =" + simNo + ","
+"location = " + location +"," + "sendTime = "
+sendTime + "," + "velocity = " + velocity + ","
+"longitude = " + longitude +"," + "latitude = "
+latitude + "," + "status = " + status + ","
+"alarmState = " + alarmState);
}
System.out.println(System.currentTimeMillis());
longend = System.currentTimeMillis();
System.out.println(end- start);
}
}
}