importjava.math.BigDecimal;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.Statement;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.HashMap;importnet.sf.ehcache.Cache;importnet.sf.ehcache.CacheManager;importnet.sf.ehcache.Element;public classMain {private static String dbURL="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK";private static String user="root";private static String pass="beautiful_mm";private static String dbDriver="com.mysql.jdbc.Driver";private static String FORMAT="yyyy-MM-dd HH:mm:ss";private static SimpleDateFormat dateFormat=newSimpleDateFormat(FORMAT);/***@paramargs*/
public static voidmain(String[] args) {//TODO Auto-generated method stub
CacheManager manager = CacheManager.create("./src/main/resources/ehcache.xml");
Cache demo= manager.getCache("demoCache");if(null==manager)
{System.out.println("manager is null? can not get ehcache manager");}else{System.out.println("get ehcache manager succeed!");}try{
// 插入10W数据,以备测试
traditionalInsert(100000);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
ehcache();
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
Cache cid= manager.getCache("TEST_ID.TEST");long begin =System.currentTimeMillis();for(int i=1;i<10001;i++)
{
Element e5120= cid.get(newLong(i));
}long end1 =System.currentTimeMillis();
System.out.println("从cache中查询10000条数据消耗时间: " + (end1-begin));for(int i=1;i<10001;i++)
{try{
getRecord(i);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}long end2 =System.currentTimeMillis();
System.out.println("用sql查询10000条数据消耗时间: " + (end2-end1));
}public static HashMap simpleCache() throwsException {
HashMap cacheid = new HashMap();
Class.forName(dbDriver);
Connection conn=DriverManager.getConnection(dbURL, user, pass);try{long begin =System.currentTimeMillis();
Statement s=conn.createStatement();
String sql= "SELECT TEST_ID,TEST_NAME,TEST_TIME,TEST_VALUE FROM TEST";
ResultSet querySet=s.executeQuery(sql);for (int i = 1; querySet.next(); i++) {
TEST curr= newTEST();
curr.TEST_ID= querySet.getLong(1);
curr.TEST_NAME= querySet.getString(2);
curr.TEST_TIME= querySet.getTimestamp(3);
curr.TEST_VALUE= querySet.getBigDecimal(4);
cacheid.put(curr.TEST_ID, curr);
}long end =System.currentTimeMillis();
System.out.printf("Time:%d\n", (end -begin));
}catch(Exception ex) {
ex.printStackTrace();
}finally{
conn.close();
}returncacheid;
}public static void traditionalInsert(int total) throwsException {
Thread.sleep(3000);
Timestamp current= newTimestamp(System.currentTimeMillis());
String currentStr=dateFormat.format(current);
System.out.println(currentStr);
Connection conn=DriverManager.getConnection(dbURL, user, pass);try{long begin =System.currentTimeMillis();
conn.setAutoCommit(false);
String sql= "INSERT INTO TEST (TEST_ID,TEST_NAME,TEST_TIME,TEST_VALUE) VALUES (?, ?, ?, ?)";
PreparedStatement ps=conn.prepareStatement(sql);for (int i = 1; i <= total; i++) {
ps.setLong(1, i);
ps.setString(2, Util.genString(33));
ps.setTimestamp(3, current);
ps.setBigDecimal(4, newBigDecimal(Util.genDouble()));
ps.addBatch();if ((i % 500) == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
System.out.println("insert !! done");
conn.commit();long end =System.currentTimeMillis();
System.out.printf("Count:%d Time:%d\n", total, (end -begin));
}catch(Exception ex) {
ex.printStackTrace();
conn.rollback();
}finally{
conn.close();
}
}public static void ehcache() throwsException {
CacheManager manager= CacheManager.create("./src/main/resources/ehcache.xml");
manager.addCache("TEST_ID.TEST");
Cache cid= manager.getCache("TEST_ID.TEST");
Class.forName(dbDriver);
Connection conn=DriverManager.getConnection(dbURL, user, pass);try{long begin =System.currentTimeMillis();
Statement s=conn.createStatement();
String sql= "SELECT TEST_ID,TEST_NAME,TEST_TIME,TEST_VALUE FROM TEST";
ResultSet querySet=s.executeQuery(sql);for (int i = 1; querySet.next(); i++) {
TEST curr= newTEST();
curr.TEST_ID= querySet.getLong(1);
curr.TEST_NAME= querySet.getString(2);
curr.TEST_TIME= querySet.getTimestamp(3);
curr.TEST_VALUE= querySet.getBigDecimal(4);
cid.put(newElement(curr.TEST_ID, curr));
}long end =System.currentTimeMillis();
System.out.printf("ehcache cost Time:%d\n", (end -begin));
}catch(Exception ex) {
ex.printStackTrace();
}finally{
conn.close();
}
}public static TEST getRecord(int id) throwsException {
TEST ret= null;
Class.forName(dbDriver);
Connection conn=DriverManager.getConnection(dbURL, user, pass);try{long begin =System.currentTimeMillis();
Statement s=conn.createStatement();
String sql= "SELECT TEST_ID,TEST_NAME,TEST_TIME,TEST_VALUE from TEST where TEST_ID="+id;
ResultSet querySet=s.executeQuery(sql);for (int i = 1; querySet.next(); i++) {
TEST curr= newTEST();
curr.TEST_ID= querySet.getLong(1);;
curr.TEST_NAME= querySet.getString(2);
curr.TEST_TIME= querySet.getTimestamp(3);
curr.TEST_VALUE= querySet.getBigDecimal(4);returncurr;//cid.put(new Element(curr.TEST_ID, curr));
}long end =System.currentTimeMillis();
System.out.printf("ehcache cost Time:%d\n", (end -begin));
}catch(Exception ex) {
ex.printStackTrace();
}finally{
conn.close();
}returnret;
}
}