springboot配置使用redis

2021-10-05 19:18:57 字數 1746 閱讀 2289

如果redis密碼為空,則redis.password不設定

自動注入stringredistemplate

@autowired

stringredistemplate stringredistemplate;

常用操作:

stringredistemplate.

opsforvalue()

.set

("test"

,"100",60

*10,timeunit.seconds)

;//向redis裡存入資料和設定快取時間

stringredistemplate.

boundvalueops

("test").

increment(-

1);//val做-1操作

stringredistemplate.

opsforvalue()

.get

("test"

)//根據key獲取快取中的val

stringredistemplate.

boundvalueops

("test").

increment(1

);//val +1

stringredistemplate.

getexpire

("test"

)//根據key獲取過期時間

stringredistemplate.

getexpire

("test"

,timeunit.seconds)

//根據key獲取過期時間並換算成指定單位

stringredistemplate.

delete

("test");

//根據key刪除快取

stringredistemplate.

haskey

("546545");

//檢查key是否存在,返回boolean值

stringredistemplate.

opsforset()

.add

("red_123"

,"1"

,"2"

,"3");

//向指定key中存放set集合

stringredistemplate.

expire

("red_123"

,1000

, timeunit.milliseconds)

;//設定過期時間

stringredistemplate.

opsforset()

.ismember

("red_123"

,"1"

)//根據key檢視集合中是否存在指定資料

stringredistemplate.

opsforset()

.members

("red_123");

//根據key獲取set集合

springboot中使用cache和redis

知識點 springboot中使用cache和redis 1 springboot中,整合了cache,我們只需要,在入口類上加 enablecaching 即可開啟快取 例如 在service層使用 cacheable和cacheevict org.springframework.boot spr...

Spring Boot 使用YAML配置

server port 8081 db configuration spring datasource driverclassname com.mysql.jdbc.driver url jdbc mysql localhost 3307 microservice username root pas...

spring boot配置使用fastjson

一 前言 spring boot預設使用jackson來操作json資料,相比於jackson,fastjson更好用,功能也強大,所以這裡記錄一下在spring boot中配置使用fastjson的方法。二 新增依賴 在pom.xml中新增以下依賴 dependency groupid com.a...