今天我們來學習一下redis,想要學習redis,首先要瞭解一個東西:NoSQL
NoSQL(NoSQL = Not Only SQL),意即「不僅僅是SQL」,是一項全新的數據庫理念,泛指非關係型的數據庫。
官方文件用了一大堆文字來描述,總結下來主要就是以下幾點:
NoSQL數據庫的四大分類如下:
Redis是用C語言開發的一個開源的高效能鍵值對(key-value)數據庫。
官方提供測試數據:50個併發執行100000個請求,讀的速度是110000次/s,寫的速度是81000次/s (數據僅供參考)。
Redis提供5種數據型別,解決不同場景下儲存問題。
1、 字串型別
2、 雜湊型別
3、 列表型別
4、 集合型別
5、 有序集合型別。
redis建議安裝在linux伺服器上執行測試,本次主要學習windows下操作。
redis-server.exe redis.windows.conf
maxmemory 120MB
maxheap 180MB
redis-server.exe --service-install
僅使用 redis-cli.exe 工具,操作效率不高,通常我們選擇安裝一款圖形化工具。
安裝軟體:redis-desktop-manager
鏈接: http://redisdesktop.com/download.
鏈接: http://www.downza.cn/soft/210734.html.
常見命令 | 描述 |
---|---|
ping | 用於測試redis服務是否連線成功,返回「PONG」表示成功 |
set | 設定字串數據,格式:set 鍵 值 |
get | 獲得字串數據,格式:get 鍵 |
del | 刪除數據,格式:del 鍵 |
Redis不僅是使用命令來操作,現在基本上主流的語言都有用戶端支援,比如java、C、C#、C++、php、Node.js、Go等。
<dependencies>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!--Jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.3</version>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
}
public class TestJedis {
public static void main(String[] args) {
//1 構建jedis物件
Jedis jedis = new Jedis("127.0.0.1",6379);
//2 存放數據
jedis.set("name","張三");
//3 獲得數據
String name = jedis.get("czxy");
//4 控制檯展示
System.out.println(name);
//5 釋放資源
jedis.close();
}
}
常見方法 | 描述 |
---|---|
opsForValue() | 操作字串 |
delete(key) | 根據key刪除記錄 |
方法 | 描述 |
---|---|
ops.set(key,value) | 向redis中插入數據。因爲這個沒有設定過期時間所以是永久儲存的。 |
ops.set(key,value,time,timeUnit); | 向redis中插入數據。第三個參數是一個long型的時間。最後一個參數是時間的單位 |
ops.get(key) | ops.get(key) |
#redis設定
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestTemplate {
@Test
public void testDemo(){
System.out.println("....");
}
}
@RunWith(SpringRunner.class) //spring整合Junit
@SpringBootTest(classes=TestApplication.class) //spring boot 整合Junit
public class TestTemplate {
@Resource
private StringRedisTemplate stringRedisTemplate;
/**
*新增數據
*/
@Test
public void demo01(){
stringRedisTemplate.opsForValue().set("java001","niubi");
}
/**
*新增數據,設定有效時間5分鐘
*/
@Test
public void demo02(){
stringRedisTemplate.opsForValue().set("java002","牛", 5 ,TimeUnit.MINUTES);
}
/**
*獲得數據
*/
@Test
public void demo03(){
String str = stringRedisTemplate.opsForValue().get("java001");
System.out.println(str);
}
/**
*刪除數據
*/
@Test
public void demo04(){
stringRedisTemplate.delete("java001");
}
}