SpringBoot使用Redis快取

2021-08-16 19:58:58 字數 1867 閱讀 1548

1.配置pom檔案

org.springframework.boot

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-cache

net.sf.ehcache

ehcache

2.修改專案啟動類,增加註解@enablecaching,開啟快取功能

3.編寫redis的配置

@configuration

@enableswagger2

@servletcomponentscan

@componentscan(basepackages = "com.xx")

public class mywebconfig extends webmvcconfigureradapter

/*** 採用rediscachemanager作為快取管理器

*/public cachemanager cachemanager(redistemplate<?, ?> redistemplate)

/*** redistemplate配置

*/@suppresswarnings()

public redistemplateredistemplate(redisconnectionfactory factory)

}

#redis

spring.redis.database=1

spring.redis.host=127.0.0.1

spring.redis.port=6379

spring.redis.password=

spring.redis.pool.max-idle=5000

spring.redis.pool.min-idle=0

spring.redis.pool.max-active=5000

spring.redis.pool.max-wait=10000

spring.redis.timeout=600

4.使用@cacheable註解就可以用了

@cacheable(value = "book", key = "#name", unless = "#result == null")

public listgetbookcache(string  name)

{return this.getbook(name);

5.在本地啟動redis

進入目錄執行 redis-server.exe redis.windows.conf

6.啟動專案,呼叫介面,檢視redis快取內容

結-redis基礎

redis一共分為五種基本資料型別:string、hash、list、set、zset

string型別是包含很多種型別的特殊型別,並且是二進位制安全的。比如序列化的物件進

行儲存,比如一張進行二進位制儲存,比如乙個簡單的字串,數值等等。

set和get方法:

設定值:set name bhz  取值 get name  (說明 設定name多次會覆蓋)

刪除值:del name

使用setnx(not exist) 

name 如果不存在進行設定,存在就不需要進行設定了,返回0 

使用setex(expired)

setex color 10 red 設定color的有效期為10秒,10秒後返回nil(在redis裡nil表示空)

使用setrange 替換字串:

set email [email protected]

setrange email 10 ww (10表示從第幾位開始替換,後面跟上替換的字串)

文章參考:

spring session使用配置redis

1.新增依賴 dependency groupid org.springframework.session groupid artifactid spring session data redis artifactid version 1.2.0.release version dependency...

使用Cacti監控MongoDB和Redis

cacti 是一套基於php,mysql,snmp及rrdtool開發的網路流量監測圖形分析工具。被廣泛的用於對伺服器的運維監控中,cacti提供了一種外掛程式式的管理,只要按要求寫好特定的模板,那麼你就可以對任何服務進行流量監控 本文就是要為大家介紹兩個模板,分別是mongodb 和redis 的...

JAVA使用pipeline批量寫Redis資料

最近遇到乙個需求,需要把資料庫中的手機號批量寫入到資料庫,使用了很多的方法都效能不佳或者出現連線池斷開的問題,最後在網上找到了這個方法 public static void main string args throws exception long end system.currenttimemi...