1. 程式人生 > 實用技巧 >【Vue入門】利用VueCli搭建基本框架--建立Http幫助類,在登陸頁面請求api資料並存儲token(四)

【Vue入門】利用VueCli搭建基本框架--建立Http幫助類,在登陸頁面請求api資料並存儲token(四)

Spring Boot 整合 Redis

引入依賴

pom檔案引入Redis依賴spring-boot-starter-data-redis

  
<!--redis依賴配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置檔案配置Redis連線資訊

  
spring:
redis:
host: localhost # Redis伺服器地址
database: 0 # Redis資料庫索引(預設為0)
port: 6379 # Redis伺服器連線埠
password: # Redis伺服器連線密碼(預設為空)
jedis:
pool:
max-active: 8 # 連線池最大連線數(使用負值表示沒有限制)
max-wait: -1ms # 連線池最大阻塞等待時間(使用負值表示沒有限制)
max-idle: 8 # 連線池中的最大空閒連線
min-idle: 0 # 連線池中的最小空閒連線
timeout: 3000ms # 連線超時時間(毫秒)

注入redis模板就可以使用了

  
@Autowired
private StringRedisTemplate stringRedisTemplate;

封裝一個Redis工具類

  
@Component
public class RedisTemplateUtil {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/** 儲存資料*/
public void set(String key, String value){
stringRedisTemplate.opsForValue().set(key,value);
}
/** 獲取資料*/
public String get(String key){
String value = stringRedisTemplate.opsForValue().get(key);
return value;
}
/** 設定超期時間*/
public boolean expire(String key, long expire){
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
/** 刪除資料*/
public void remove(String key){
stringRedisTemplate.delete(key);
}
/**
* 自增操作
* @param delta 自增步長
*/
public Long increment(String key, long delta){
return stringRedisTemplate.opsForValue().increment(key, delta);
}
}
      @Autowired
private RedisTemplateUtil redisTemplateUtil;
...
redisTemplateUtil.set(key, value);
...

更多面試資料,JDK8中文文件,阿里巴巴Java開發手冊,pdf書籍,視訊,公號《Java路》