Reids簡單介紹
Redis是一個基於內存的單線程高性能key-value型數據庫,讀寫性能優異。
Reids的安裝和使用
官網http://redis.io/提供的是linux版本的,如果需要windows版本需要從網上下載。官網下載是在太慢了,我足足花了半小時。爲了方便讀者獲取,我將相關的資源放在了公衆號上,大家可以關注下面的公衆號下載。
雙擊redis-server.exe啓動服務,服務啓動成功圖如下
雙擊redis-cli.exe進行測試(列出String,List和Set的常用操作)
String:
添加和更新:set name ‘Theshy’
查詢:get name
查詢對應的key是否存在(0不存在,其它就存在):exists name
刪除:del name
List:
頭部添加:lpush list ‘Ning’ 22
尾部添加:rpush list ‘Junny’ 21
查詢集合:lrange list 0 - 1
更新index爲0的值:lset list 0 ‘gg’
刪除index爲0的值:lrem list 0 ‘gg’
通用的其它指令:
列出所有key:keys *
清空所有:flushall
SpringBoot整合Redis
- 引入依賴
在Spring Boot 集成 Redis 首先需要在 pom xml 文件中引入所需的依賴,具體代碼如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 添加緩存配置(自行選擇自己需要的配置方式)
properties配置文件方式
### redis 緩存配置
#默認redis數據庫爲 0
spring.redis.database=0
#服務器地址,默認爲localhost
spring.redis.host=localhost
#鏈接端口,默認爲 6379
spring.redis.port=6379
#redis密碼默認爲空
spring.redis.password=
yml配置文件方式
#緩存配置
spring:
redis:
#默認redis數據庫爲 0
database: 0
#服務器地址,默認爲localhost
host: localhost
#鏈接端口,默認爲 6379
port: 6379
#redis密碼默認爲空
password:
- 測試
在測試類添加如下代碼:
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration
public class SecurityApplicationTests {
@Resource
private RedisTemplate redisTemplate;
@Test
public void contextLoads() {
//增加一組鍵值對數據
redisTemplate.opsForValue().set("Jklove",20);
Integer age= (Integer) redisTemplate.opsForValue().get("Jklove");
System.out.println(age);
if(redisTemplate.delete("Jklove")){
System.out.println("成功刪除");
}
}
}
運行效果圖:
今天的內容分享到這裏就結束了,感謝耐心閱讀。
每天進步一點點,開心也多一點點