生活随笔
收集整理的這篇文章主要介紹了
Redisson--------基础入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、前言
之前用的Redis,都是用的原生的RedisTempale或者是StringRedisTemplate,各種API非常的難易記憶,每次用的時候還得去網上查詢API文檔,而Redisson的宗旨是促進使用者對Redis的關注分離(Separation of Concern),從而讓使用者能夠將精力更集中地放在處理業務邏輯上。這里放上redisson的github鏈接,先說明,redisson除了開源版本,還有付費的pro版本,以后介紹的某些高級功能,如果是pro版本的,會標出
二、在SpringBoot中快速集成
Redisson是建立在Redis的基礎之上的,所以需要在SpringBoot項目中引入Redis。
< dependency> < groupId> org.redisson
</ groupId> < artifactId> redisson-spring-boot-starter
</ artifactId> < version> 3.16.3
</ version>
</ dependency> -----------------------------------------------------------------
< dependency> < groupId> org.redisson
</ groupId> < artifactId> redisson
</ artifactId> < version> 3.16.3
</ version>
</ dependency>
在SpringBoot中配置。官方文檔給出了基于配置類和基于配置文件的形式,還有基于集群,基于主從,基于單節點模式的配置方式都不一樣,我這里給出基于配置文件的單節點模式,其他形式參考官方文檔。
redisson.yml文件
singleServerConfig : idleConnectionTimeout : 10000 connectTimeout : 10000 timeout : 3000 retryAttempts : 3 retryInterval : 1500 password : null clientName : null subscriptionsPerConnection : 5 address : "redis://xxx.xxx.xxx.xxx:6379" subscriptionConnectionMinimumIdleSize : 1 subscriptionConnectionPoolSize : 50 database : 2 dnsMonitoringInterval : 5000 sslEnableEndpointIdentification : true
threads : 0
nettyThreads : 0
codec : !<org.redisson.codec.JsonJacksonCodec> { }
"transportMode" : "NIO"
RedisConfig
@Configuration
public class RedissonConfig { @Bean ( destroyMethod
= "shutdown" ) public RedissonClient getRedissonClient ( ) throws IOException { ResourceLoader loader
= new DefaultResourceLoader ( ) ; Resource resource
= loader
. getResource ( "redisson.yml" ) ; Config config
= Config . fromYAML ( resource
. getInputStream ( ) ) ; return Redisson . create ( config
) ; }
}
三、增刪改查操作
每個Redisson對象實例都會有一個與之對應的Redis數據實例。Redisson的分布式RBucket對象是一種通用對象桶可以用來存放任類型的對象。先來寫新增和查詢。先通過redisson對象通過getBucket方法,在Redis中創建一個字符串類型的對象。這個對象的引用就是下面的bucket,它可以存放任意的Cityr對象實體。RBucket桶的神奇功能是:可直接存儲City對象,省略了我們的City對象轉為JSON字符串的過程。RBucket對象可以視為Redis中的 String類型,每個RBucket對象對應著一個值。
新增:將City對象通過RBucket存放到Redis中。 查詢的時候,通過KEY找到桶。再通過get() 方法取出緩存在Redis中的 指定的KEY的VALUE。 修改就是通過KEY找到這個桶RBucket,然后重新SET一下。 刪除更簡單了,找到這個桶,調用delete相關方法即可。
@Controller
@RequestMapping ( "redisson" )
public class RedissonController { @Autowired RedissonClient redissonClient
; @ResponseBody @GetMapping ( "get/{id}" ) public City get ( @PathVariable Long id
) { RBucket < City > city
= redissonClient
. getBucket ( "city" + id
. toString ( ) ) ; return city
. get ( ) ; } @ResponseBody @DeleteMapping ( "delete/{id}" ) public String delete ( @PathVariable Long id
) { RBucket < City > bucket
= redissonClient
. getBucket ( "city" + id
. toString ( ) ) ; Optional < City > optionalCity
= Optional . ofNullable ( bucket
. getAndDelete ( ) ) ; return optionalCity
. toString ( ) ; } @ResponseBody @PutMapping ( "update" ) public String update ( @RequestBody City city
) { RBucket < City > bucket
= redissonClient
. getBucket ( "city" + city
. getId ( ) . toString ( ) ) ; if ( bucket
!= null ) { bucket
. set ( city
) ; return "ok" ; } else return "error" ; } @ResponseBody @PostMapping ( "add" ) public String add ( @RequestBody City city
) { RBucket < City > bucket
= redissonClient
. getBucket ( "city" + city
. getId ( ) . toString ( ) ) ; try { bucket
. set ( city
) ; return "ok" ; } catch ( Exception e
) { e
. printStackTrace ( ) ; return "error" ; } }
}
總結
以上是生活随笔 為你收集整理的Redisson--------基础入门 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。