因为代码中需要用到分布式,同步数据,在网上查了都说redis比mysql快,具体测试了一下,果然如此。
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.google.gson.Gson;
import com.zhaohm.redis.entity.RedisEntity;
import com.zhaohm.redis.repository.TestRepoistory;
import com.zhaohm.redis.service.MyTest;
import redis.clients.jedis.Jedis;
import org.junit.Before;
import org.junit.Test;
import net.sf.json.JSONObject;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)// 指定spring-boot的启动类
public class TestMysqlAndRedis {
/* *//**
* Administrator
* @param args
*/
@Autowired
private TestRepoistory TestRepoistory;
private Log log=LogFactory.getLog(TestMysqlAndRedis.class);
@Before
public void initUser(){
}
@Test
public void getInfoTest(){
long time0 = new Date().getTime();
RedisEntity test=new RedisEntity();
for(int i=0;i<100;i++){
test.setId(i+1);
test.setTrainIndex(i);
test.setCarNum(i);
TestRepoistory.save(test);
}
long time1 = new Date().getTime();
System.out.println("=sql存数据==="+(time1-time0));
MyTest mytest=new MyTest();
RedisEntity one = TestRepoistory.findById(50);
long time2 = new Date().getTime();
System.out.println("之后=sql查询数据==="+(time2-time1));
// 操作单独的文本串
Jedis jedis= new Jedis( "10.5.133.219", 6380);
Map<String,String> testMap=new HashMap<String,String>();
for(int i=0;i<100;i++){
testMap.put(i+"", i+"");
}
long time3 = new Date().getTime();
JSONObject jsonObject = JSONObject.fromObject(testMap);
//System.out.println("输出的结果是:" + jsonObject);
//3、将json对象转化为json字符串
String result = jsonObject.toString();
//System.out.println(result);
jedis.set("101", result);
long time4 = new Date().getTime();
System.out.println("\t===resid存数据----"+(time4-time3));
String getResult = jedis.get("101");
Gson gson = new Gson();
Map<String, Object> map = new HashMap<String, Object>();
map = gson.fromJson(getResult, map.getClass());
String ll = (String) map.get("50");
//Boolean ll = jedis.exists("50");
long time5 = new Date().getTime();
System.out.println(ll+"\t===resid查询数据==="+(time5-time4));
}
}
得到的结果:
=sql存数据===2256
之后=sql查询数据===181
===resid存数据—-217
===resid查询数据===6
这里redis存的数据时map转换的字符串,mysql是循环,所以又测了一下循环放入redis的
Jedis jedis= new Jedis( "10.5.133.219", 6380);
long time3 = new Date().getTime();
for(int i=0;i<100;i++){
jedis.set(i+"", i+"");
}
long time4 = new Date().getTime();
System.out.println("\t===resid存数据----"+(time4-time3));
结果:===resid存数据—-76
所以,可见,redis果然是快的
这里还有个知识点:
第一:map转换为字符串以及将字符串转换为map
JSONObject jsonObject = JSONObject.fromObject(testMap);
System.out.println("输出的结果是:" + jsonObject);
//3、将json对象转化为json字符串
String result = jsonObject.toString();
这就将testMap转换为字符串result
第二:String转换为map
Gson gson = new Gson();
Map<String, Object> map = new HashMap<String, Object>();
map = gson.fromJson(getResult, map.getClass());
这就将字符串转换为map
这里要用JSONObject,需要导包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>