SSM中操作Redis——Jedis
1、Jedis
- jedis是基于java的redis客戶端,集成了redis的命令操作,提供了連接池管理
- jedis的方法就是redis的命令
2、匯入依賴
<!--jedis依賴-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
3、簡單使用
@Test
public void quickTest() throws Exception {
// 引數一:redis服務器ip
// 引數二:redis服務器的埠號
Jedis jedis = new Jedis("192.168.27.132", 6379);
String name = jedis.set("name", "zhangsan");
System.out.println("name = " + name);
String value = https://www.cnblogs.com/lzy-blogs/p/jedis.get("name");
System.out.println("value = "https://www.cnblogs.com/lzy-blogs/p/+ value);
}
4、連接池使用
@Test
public void JedisPoolTestDemo() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
String host = "192.168.27.132";
int port = 6379;
int timeout = 5000;
//產生連接池物件
JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout);
Jedis jedis = jedisPool.getResource();
jedis.set("name","張三");
jedis.set("name1","張三1");
jedis.set("name2","張三2");
jedis.hset("person","name","李四");
jedis.hset("person","name1","李四1");
jedis.hset("person","name2","李四2");
System.out.println(jedis.get("name"));
System.out.println(jedis.get("name1"));
System.out.println(jedis.get("name2"));
Map<String, String> person = jedis.hgetAll("person");
System.out.println("person = " + person);
//關閉后歸還到連接池
jedis.close();
}
5、工具類封裝連接池
public class JedisPoolUtils {
private static JedisPool jedisPool = null;
private static final String HOST = "192.168.27.132";
private static final int PORT = 6379;
private static final int TIMEOUT = 3000;
static {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
jedisPool = new JedisPool(poolConfig,HOST,PORT,TIMEOUT);
}
public static Jedis getResource() {
return jedisPool.getResource();
}
}
6、連接池工具類使用
Jedis jedis = JedisPoolUtils.getResource(); //在連接池中獲取jedis連接物件
String name = jedis.get("name");
System.out.println("name = " + name);
jedis.close();//關閉并歸還連接
7、Jedis保存一個物件
- 可以通過json保存
- 也可以通過位元組存盤
7.1 通過json存盤
思路:將創建好的物件轉成json格式字串,存盤到redis中
//創建物件
Device device = new Device();
device.setId(1);
device.setDeviceName("挖掘機");
device.setPrice(30000D);
//json轉換
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(device);
//獲取jedis物件
Jedis jedis = JedisPoolUtils.getResource();
//存盤物件
jedis.set("device:" + device.getId(), json);
String deviceString = jedis.get("device:" + device.getId());
System.out.println("deviceString = " + deviceString);
//歸還連接到連接池
jedis.close();
7.2 位元組存盤
思路:
-
將創建好的物件序列化為位元組陣列,存盤到redis中
-
從redis中獲取位元組陣列,并將位元組陣列反序列化為物件
//創建物件
Device device = new Device();
device.setId(1);
device.setDeviceName("挖掘機");
device.setPrice(30000D);
//獲取jedis物件
Jedis jedis = JedisPoolUtils.getResource();
//物件轉換為位元組陣列,使用SerializationUtils的serialize()方法
byte[] key = ("device:" + device.getId()).getBytes();
byte[] value = https://www.cnblogs.com/lzy-blogs/p/SerializationUtils.serialize(device);
//存盤位元組
jedis.set(key, value);
//獲取key對應的value
byte[] bytes = jedis.get(key);
//SerializationUtils的deserialize()方法 將位元組轉為物件
Device device1 = (Device) SerializationUtils.deserialize(bytes);
System.out.println(device1);
//歸還連接到連接池
jedis.close();
8、Jedis管道操作
當我們有10000條資料要存盤到redis中,使用原始方式存:
@Test
public void tenThousandTest(){
Jedis jedis = JedisPoolUtils.getResource();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
jedis.set("i:"+i,"v:"+i);
}
System.out.println(System.currentTimeMillis()-start);//大概4秒
jedis.close();
}
當執行完之后得到時間差,發現用時大概4秒,
為什么redis讀寫性能優異,卻花費了4秒鐘?
使用過查看redis性能命令的小伙伴會發現讀寫的性能能達到100000次/s
據官方的bench-mark資料:讀的速度是110000次/s,寫的速度是81000次/s
#redis寫的性能
redis-benchmark set
#redis讀的性能
redis-benchmark get
那為什么在這里執行10000次寫卻用了4秒鐘,是不是redis性能不靠譜啊
其實不然,我們仔細想一下,我們的jedis程式和redis服務器是在不同服務器上,
那Jedis的資料要保存到redis中就要通過網路傳輸
對于上面代碼而言,每寫一個就要傳一個到redis中,一共要發送10000次,這之間消耗的時間是比較久的,也就對代碼執行完之后計算得出的時間產生了影響,
- 其實redis的讀寫性能是ok的,其瓶頸在于傳輸性能上,
那有什么提升jedis傳輸性能的方法呢
有,Jedis提供了一個pipelined(),也就是管道操作,我們來看升級后的代碼
@Test
public void test(){
Jedis jedis = JedisPoolUtils.getResource();
Long start = System.currentTimeMillis();
Pipeline pipelined = jedis.pipelined();
for (int i = 0; i < 10000; i++) {
pipelined.set("k:"+i,"v:"+i);
}
System.out.println(System.currentTimeMillis()-start);//55毫秒
jedis.close();
}
最后測驗發現執行幾乎瞬間完成,用時55毫秒,比4秒提升了70多倍,
為什么管道操作那么快
它將所有操作都打包,一次性發給redis服務器,由原先一萬次的傳輸變為一次傳輸,大大提高了傳輸性能,節省了時間,
而redis接收到之后做一萬次寫入,一萬次寫入對于redis來說小菜一碟,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495436.html
標籤:Java
上一篇:Redis的五種基本資料型別
下一篇:Java中使用正則檢查有效日期
