0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

Springboot+redis操作多種實(shí)現(xiàn)

Android編程精選 ? 來源:CSDN技術(shù)社區(qū) ? 作者:Tonels ? 2021-09-22 10:48 ? 次閱讀

一、Jedis,Redisson,Lettuce三者的區(qū)別共同點(diǎn):都提供了基于Redis操作的Java API,只是封裝程度,具體實(shí)現(xiàn)稍有不同。

不同點(diǎn):

1.1、Jedis

是Redis的Java實(shí)現(xiàn)的客戶端。支持基本的數(shù)據(jù)類型如:String、Hash、List、Set、Sorted Set。

特點(diǎn):使用阻塞的I/O,方法調(diào)用同步,程序流需要等到socket處理完I/O才能執(zhí)行,不支持異步操作。Jedis客戶端實(shí)例不是線程安全的,需要通過連接池來使用Jedis。

1.2、Redisson

優(yōu)點(diǎn)點(diǎn):分布式鎖,分布式集合,可通過Redis支持延遲隊(duì)列。

1.3、 Lettuce

用于線程安全同步,異步和響應(yīng)使用,支持集群,Sentinel,管道和編碼器

基于Netty框架的事件驅(qū)動(dòng)的通信層,其方法調(diào)用是異步的。Lettuce的API是線程安全的,所以可以操作單個(gè)Lettuce連接來完成各種操作。

二、RedisTemplate2.1、使用配置

maven配置引入,(要加上版本號(hào),我這里是因?yàn)镻arent已聲明)


	

<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId> dependency>

application-dev.yml


	

spring: redis: host:192.168.1.140 port:6379 password: database:15#指定redis的分庫(共16個(gè)0到15)

2.2、使用示例


	

@Resource privateStringRedisTemplatestringRedisTemplate; @Override publicCustomersEntityfindById(Integerid){ //需要緩存 //所有涉及的緩存都需要?jiǎng)h除,或者更新 try{ StringtoString=stringRedisTemplate.opsForHash().get(REDIS_CUSTOMERS_ONE,id+"").toString(); if(toString!=null){ returnJSONUtil.toBean(toString,CustomersEntity.class); } }catch(Exceptione){ e.printStackTrace(); } //緩存為空的時(shí)候,先查,然后緩存redis OptionalbyId=customerRepo.findById(id); if(byId.isPresent()){ CustomersEntitycustomersEntity=byId.get(); try{ stringRedisTemplate.opsForHash().put(REDIS_CUSTOMERS_ONE,id+"",JSONUtil.toJsonStr(customersEntity)); }catch(Exceptione){ e.printStackTrace(); } returncustomersEntity; } returnnull; }

2.3、擴(kuò)展

2.3.1、spring-boot-starter-data-redis的依賴包

3.3.2、stringRedisTemplate API(部分展示)

opsForHash --》 hash操作

opsForList --》 list操作

opsForSet --》 set操作

opsForValue --》 string操作

opsForZSet --》 Zset操作

3.3.3 StringRedisTemplate默認(rèn)序列化機(jī)制


	

publicclassStringRedisTemplateextendsRedisTemplate<String,String>{ /** *ConstructsanewStringRedisTemplateinstance.{@link#setConnectionFactory(RedisConnectionFactory)} *and{@link#afterPropertiesSet()}stillneedtobecalled. */ publicStringRedisTemplate(){ RedisSerializerstringSerializer=newStringRedisSerializer(); setKeySerializer(stringSerializer); setValueSerializer(stringSerializer); setHashKeySerializer(stringSerializer); setHashValueSerializer(stringSerializer); } }

三、RedissonClient 操作示例

3.1 基本配置

3.1.1、Maven pom 引入

	

<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-redisartifactId> dependency> <dependency> <groupId>org.redissongroupId> <artifactId>redissonartifactId> <version>3.8.2version> <optional>trueoptional> dependency> <dependency> <groupId>org.redissongroupId> <artifactId>redisson-spring-boot-starterartifactId> <version>LATESTversion> dependency>

3.1.2、添加配置文件Yaml或者json格式

redisson-config.yml


	

#Redisson配置 singleServerConfig: address:"redis://192.168.1.140:6379" password:null clientName:null database:15#選擇使用哪個(gè)數(shù)據(jù)庫0~15 idleConnectionTimeout:10000 pingTimeout:1000 connectTimeout:10000 timeout:3000 retryAttempts:3 retryInterval:1500 reconnectionTimeout:3000 failedAttempts:3 subscriptionsPerConnection:5 subscriptionConnectionMinimumIdleSize:1 subscriptionConnectionPoolSize:50 connectionMinimumIdleSize:32 connectionPoolSize:64 dnsMonitoringInterval:5000 #dnsMonitoring:false threads:0 nettyThreads:0 codec: class:"org.redisson.codec.JsonJacksonCodec" transportMode:"NIO"

或者,配置 redisson-config.json


	

{ "singleServerConfig":{ "idleConnectionTimeout":10000, "pingTimeout":1000, "connectTimeout":10000, "timeout":3000, "retryAttempts":3, "retryInterval":1500, "reconnectionTimeout":3000, "failedAttempts":3, "password":null, "subscriptionsPerConnection":5, "clientName":null, "address":"redis://192.168.1.140:6379", "subscriptionConnectionMinimumIdleSize":1, "subscriptionConnectionPoolSize":50, "connectionMinimumIdleSize":10, "connectionPoolSize":64, "database":0, "dnsMonitoring":false, "dnsMonitoringInterval":5000 }, "threads":0, "nettyThreads":0, "codec":null, "useLinuxNativeEpoll":false }

3.1.3、讀取配置

新建讀取配置類


	

@Configuration publicclassRedissonConfig{ @Bean publicRedissonClientredisson()throwsIOException{ //兩種讀取方式,Config.fromYAML和Config.fromJSON //Configconfig=Config.fromJSON(RedissonConfig.class.getClassLoader().getResource("redisson-config.json")); Configconfig=Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-config.yml")); returnRedisson.create(config); } }

或者,在 application.yml中配置如下


	

spring: redis: redisson: config:classpath:redisson-config.yaml

3.2 使用示例


	

@RestController @RequestMapping("/") publicclassTeController{ @Autowired privateRedissonClientredissonClient; staticlongi=20; staticlongsum=300; //==========================String======================= @GetMapping("/set/{key}") publicStrings1(@PathVariableStringkey){ //設(shè)置字符串 RBucketkeyObj=redissonClient.getBucket(key); keyObj.set(key+"1-v1"); returnkey; } @GetMapping("/get/{key}") publicStringg1(@PathVariableStringkey){ //設(shè)置字符串 RBucketkeyObj=redissonClient.getBucket(key); Strings=keyObj.get(); returns; } //==========================hash=======================-= @GetMapping("/hset/{key}") publicStringh1(@PathVariableStringkey){ Urur=newUr(); ur.setId(MathUtil.randomLong(1,20)); ur.setName(key); //存放Hash RMapss=redissonClient.getMap("UR"); ss.put(ur.getId().toString(),ur); returnur.toString(); } @GetMapping("/hget/{id}") publicStringh2(@PathVariableStringid){ //hash查詢 RMapss=redissonClient.getMap("UR"); Urur=ss.get(id); returnur.toString(); } //查詢所有的keys @GetMapping("/all") publicStringall(){ RKeyskeys=redissonClient.getKeys(); Iterablekeys1=keys.getKeys(); keys1.forEach(System.out::println); returnkeys.toString(); } //================================讀寫鎖測(cè)試============================= @GetMapping("/rw/set/{key}") publicvoidrw_set(){ //RedissonLock. RBucketls_count=redissonClient.getBucket("LS_COUNT"); ls_count.set("300",360000000l,TimeUnit.SECONDS); } //減法運(yùn)算 @GetMapping("/jf") publicvoidjf(){ Stringkey="S_COUNT"; //RAtomicLongatomicLong=redissonClient.getAtomicLong(key); //atomicLong.set(sum); //longl=atomicLong.decrementAndGet(); //System.out.println(l); RAtomicLongatomicLong=redissonClient.getAtomicLong(key); if(!atomicLong.isExists()){ atomicLong.set(300l); } while(i==0){ if(atomicLong.get()>0){ longl=atomicLong.getAndDecrement(); try{ Thread.sleep(1000l); }catch(InterruptedExceptione){ e.printStackTrace(); } i--; System.out.println(Thread.currentThread().getName()+"->"+i+"->"+l); } } } @GetMapping("/rw/get") publicStringrw_get(){ Stringkey="S_COUNT"; Runnabler=newRunnable(){ @Override publicvoidrun(){ RAtomicLongatomicLong=redissonClient.getAtomicLong(key); if(!atomicLong.isExists()){ atomicLong.set(300l); } if(atomicLong.get()>0){ longl=atomicLong.getAndDecrement(); i--; System.out.println(Thread.currentThread().getName()+"->"+i+"->"+l); } } }; while(i!=0){ newThread(r).start(); //newThread(r).run(); //newThread(r).run(); //newThread(r).run(); //newThread(r).run(); } RBucketbucket=redissonClient.getBucket(key); Strings=bucket.get(); System.out.println("================線程已結(jié)束================================"+s); returns; } }

4.3 擴(kuò)展

4.3.1 豐富的jar支持,尤其是對(duì) Netty NIO框架

4.3.2 豐富的配置機(jī)制選擇,這里是詳細(xì)的配置說明

https://github.com/redisson/redisson/wiki/2.-Configuration

關(guān)于序列化機(jī)制中,就有很多

ebfcba9a-1668-11ec-8fb8-12bb97331649.pngec0676fc-1668-11ec-8fb8-12bb97331649.png

4.3.3 API支持(部分展示),具體的 Redis --> RedissonClient ,可查看這里

https://github.com/redisson/redisson/wiki/11.-Redis-commands-mapping

ec127c54-1668-11ec-8fb8-12bb97331649.png


4.3.4 輕便的豐富的鎖機(jī)制的實(shí)現(xiàn)

Lock

Fair Lock

MultiLock

RedLock

ReadWriteLock

Semaphore

PermitExpirableSemaphore

CountDownLatch

四、基于注解實(shí)現(xiàn)的Redis緩存4.1 Maven 和 YML配置

參考 RedisTemplate 配置。另外,還需要額外的配置類


	

//todo定義序列化,解決亂碼問題 @EnableCaching @Configuration @ConfigurationProperties(prefix="spring.cache.redis") publicclassRedisCacheConfig{ privateDurationtimeToLive=Duration.ZERO; publicvoidsetTimeToLive(DurationtimeToLive){ this.timeToLive=timeToLive; } @Bean publicCacheManagercacheManager(RedisConnectionFactoryfactory){ RedisSerializerredisSerializer=newStringRedisSerializer(); Jackson2JsonRedisSerializerjackson2JsonRedisSerializer=newJackson2JsonRedisSerializer(Object.class); //解決查詢緩存轉(zhuǎn)換異常的問題 ObjectMapperom=newObjectMapper(); om.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); //配置序列化(解決亂碼的問題) RedisCacheConfigurationconfig=RedisCacheConfiguration.defaultCacheConfig() .entryTtl(timeToLive) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManagercacheManager=RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); returncacheManager; } }

4.2 使用示例


	

@Transactional @Service publicclassReImplimplementsRedisService{ @Resource privateCustomerRepocustomerRepo; @Resource privateStringRedisTemplatestringRedisTemplate; publicstaticfinalStringREDIS_CUSTOMERS_ONE="Customers"; publicstaticfinalStringREDIS_CUSTOMERS_ALL="allList"; //=====================================================================使用Springcahce注解方式實(shí)現(xiàn)緩存 //==================================單個(gè)操作 @Override @Cacheable(value="cache:customer",unless="null==#result",key="#id") publicCustomersEntitycacheOne(Integerid){ finalOptionalbyId=customerRepo.findById(id); returnbyId.isPresent()?byId.get():null; } @Override @Cacheable(value="cache:customer",unless="null==#result",key="#id") publicCustomersEntitycacheOne2(Integerid){ finalOptionalbyId=customerRepo.findById(id); returnbyId.isPresent()?byId.get():null; } //todo自定義redis緩存的key, @Override @Cacheable(value="cache:customer",unless="null==#result",key="#root.methodName+'.'+#id") publicCustomersEntitycacheOne3(Integerid){ finalOptionalbyId=customerRepo.findById(id); returnbyId.isPresent()?byId.get():null; } //todo這里緩存到redis,還有響應(yīng)頁面是String(加了很多轉(zhuǎn)義符,),不是Json格式 @Override @Cacheable(value="cache:customer",unless="null==#result",key="#root.methodName+'.'+#id") publicStringcacheOne4(Integerid){ finalOptionalbyId=customerRepo.findById(id); returnbyId.map(JSONUtil::toJsonStr).orElse(null); } //todo緩存json,不亂碼已處理好,調(diào)整序列化和反序列化 @Override @Cacheable(value="cache:customer",unless="null==#result",key="#root.methodName+'.'+#id") publicCustomersEntitycacheOne5(Integerid){ OptionalbyId=customerRepo.findById(id); returnbyId.filter(obj->!StrUtil.isBlankIfStr(obj)).orElse(null); } //==================================刪除緩存 @Override @CacheEvict(value="cache:customer",key="'cacheOne5'+'.'+#id") publicObjectdel(Integerid){ //刪除緩存后的邏輯 returnnull; } @Override @CacheEvict(value="cache:customer",allEntries=true) publicvoiddel(){ } @CacheEvict(value="cache:all",allEntries=true) publicvoiddelall(){ } //==================List操作 @Override @Cacheable(value="cache:all") publicListcacheList(){ Listall=customerRepo.findAll(); returnall; } //todo先查詢緩存,再校驗(yàn)是否一致,然后更新操作,比較實(shí)用,要清楚緩存的數(shù)據(jù)格式(明確業(yè)務(wù)和緩存模型數(shù)據(jù)) @Override @CachePut(value="cache:all",unless="null==#result",key="#root.methodName") publicListcacheList2(){ Listall=customerRepo.findAll(); returnall; } }

4.3 擴(kuò)展

基于spring緩存實(shí)現(xiàn)

來源:blog.csdn.net/qq_42105629/article/details/102589319

編輯:jq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • spring
    +關(guān)注

    關(guān)注

    0

    文章

    335

    瀏覽量

    14277
  • Boot
    +關(guān)注

    關(guān)注

    0

    文章

    149

    瀏覽量

    35731
  • Redis
    +關(guān)注

    關(guān)注

    0

    文章

    369

    瀏覽量

    10810
  • SpringBoot
    +關(guān)注

    關(guān)注

    0

    文章

    173

    瀏覽量

    153

原文標(biāo)題:Spring Boot 操作 Redis 的各種實(shí)現(xiàn)

文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    redis使用多線程處理操作命令

    Redis 多線程處理操作命令的實(shí)現(xiàn)和優(yōu)勢(shì),幫助讀者深入了解這一方面的知識(shí)。 首先,我們來了解一下 Redis 的基本概念和工作原理。Redis
    的頭像 發(fā)表于 12-05 10:25 ?502次閱讀

    redis數(shù)據(jù)結(jié)構(gòu)的底層實(shí)現(xiàn)

    Redis是一種內(nèi)存鍵值數(shù)據(jù)庫,常用于緩存、消息隊(duì)列、實(shí)時(shí)數(shù)據(jù)分析等場(chǎng)景。它的高性能得益于其精心設(shè)計(jì)的數(shù)據(jù)結(jié)構(gòu)和底層實(shí)現(xiàn)。本文將詳細(xì)介紹Redis常用的數(shù)據(jù)結(jié)構(gòu)和它們的底層實(shí)現(xiàn)。
    的頭像 發(fā)表于 12-05 10:14 ?554次閱讀

    redis的主要方法

    Redis是一種基于內(nèi)存的開源鍵值對(duì)存儲(chǔ)系統(tǒng),常用于緩存、消息中間件、數(shù)據(jù)庫等場(chǎng)景。作為一個(gè)高性能的NoSQL存儲(chǔ)解決方案,Redis提供了豐富的方法用于操作數(shù)據(jù)。本文將詳細(xì)介紹Redis
    的頭像 發(fā)表于 12-05 09:59 ?728次閱讀

    redis的increment方法

    實(shí)現(xiàn)對(duì)存儲(chǔ)在數(shù)據(jù)庫中的特定鍵的遞增操作。在本文中,我們將詳細(xì)介紹Redis的 INCR 方法,包括其原理、使用方法以及一些常見的應(yīng)用場(chǎng)景。 首先,我們來看看Redis的 INCR 方法
    的頭像 發(fā)表于 12-05 09:57 ?1028次閱讀

    redis的原理和使用場(chǎng)景

    Redis(Remote Dictionary Server)是一個(gè)開源的、高性能的非關(guān)系型(NoSQL)的鍵值對(duì)數(shù)據(jù)庫管理系統(tǒng)。它以其快速讀寫能力和多種數(shù)據(jù)結(jié)構(gòu)支持而聞名,并被廣泛應(yīng)用于緩存
    的頭像 發(fā)表于 12-04 16:29 ?532次閱讀

    redis hash底層實(shí)現(xiàn)原理

    數(shù)據(jù)結(jié)構(gòu)是如何實(shí)現(xiàn)的呢?本文將詳細(xì)介紹Redis哈希底層的實(shí)現(xiàn)原理。 在Redis中,每個(gè)哈希都是由一個(gè)類似于字典(Dictionary)的結(jié)構(gòu)實(shí)現(xiàn)
    的頭像 發(fā)表于 12-04 16:27 ?521次閱讀

    redis的持久化方式RDB和AOF的區(qū)別

    Redis 是一個(gè)高性能的鍵值對(duì)數(shù)據(jù)庫,提供了兩種持久化方式:RDB 和 AOF。RDB 是將 Redis 的數(shù)據(jù)快照保存到磁盤上,而 AOF 則是將 Redis操作命令追加到文件
    的頭像 發(fā)表于 12-04 16:25 ?685次閱讀

    如何實(shí)現(xiàn)Redis分布式鎖

    Redis是一個(gè)開源的內(nèi)存數(shù)據(jù)存儲(chǔ)系統(tǒng),可用于高速讀寫操作。在分布式系統(tǒng)中,為了保證數(shù)據(jù)的一致性和避免競(jìng)態(tài)條件,常常需要使用分布式鎖來對(duì)共享資源進(jìn)行加鎖操作Redis提供了一種簡單而
    的頭像 發(fā)表于 12-04 11:24 ?603次閱讀

    redis鎖機(jī)制原理

    。 Redis鎖機(jī)制的原理主要涉及以下三個(gè)要素:互斥性、阻塞操作和超時(shí)處理。 互斥性:Redis的鎖機(jī)制通過使用SETNX命令來實(shí)現(xiàn)。SETNX命令用于設(shè)置一個(gè)鍵的值,只有在鍵不存在的
    的頭像 發(fā)表于 12-04 11:08 ?1114次閱讀

    Java redis鎖怎么實(shí)現(xiàn)

    在Java中實(shí)現(xiàn)Redis鎖涉及到以下幾個(gè)方面:Redis的安裝配置、Redis連接池的使用、Redis數(shù)據(jù)結(jié)構(gòu)的選擇、
    的頭像 發(fā)表于 12-04 10:47 ?1004次閱讀

    redis集群狀態(tài)查看命令

    Redis集群是一種高可用性的分布式架構(gòu),可以通過多個(gè)節(jié)點(diǎn)實(shí)現(xiàn)數(shù)據(jù)的復(fù)制和負(fù)載均衡。為了維護(hù)集群的穩(wěn)定性和可靠性,管理員需要監(jiān)控和查看集群的狀態(tài)。下面是詳細(xì)介紹Redis集群狀態(tài)查看命令
    的頭像 發(fā)表于 12-04 10:44 ?1136次閱讀

    Redis工具集的實(shí)現(xiàn)和使用

    Redis 基本上是互聯(lián)網(wǎng)公司必備的工具了,Redis的應(yīng)用場(chǎng)景實(shí)在太多了,但是有很多相似的功能如果每個(gè)項(xiàng)目都要實(shí)現(xiàn)一遍就顯得太麻煩了,所以為了方便,我打算開發(fā)一個(gè)基于 Redis
    的頭像 發(fā)表于 12-03 17:32 ?1116次閱讀
    <b class='flag-5'>Redis</b>工具集的<b class='flag-5'>實(shí)現(xiàn)</b>和使用

    一個(gè)注解搞定SpringBoot接口防刷

    技術(shù)要點(diǎn):springboot的基本知識(shí),redis基本操作,
    的頭像 發(fā)表于 11-28 10:46 ?368次閱讀

    redis分布式鎖如何實(shí)現(xiàn)

    Redis分布式鎖是一種基于Redis實(shí)現(xiàn)的機(jī)制,可以用于多個(gè)進(jìn)程或多臺(tái)服務(wù)器之間對(duì)共享資源的并發(fā)訪問控制。在分布式系統(tǒng)中,由于多個(gè)進(jìn)程或多臺(tái)服務(wù)器同時(shí)訪問共享資源,可能會(huì)發(fā)生數(shù)據(jù)競(jìng)爭(zhēng)和資源沖突
    的頭像 發(fā)表于 11-16 11:29 ?472次閱讀

    如何利用SpringBoot+Redis BitMap實(shí)現(xiàn)簽到與統(tǒng)計(jì)功能?

    在各個(gè)項(xiàng)目中,我們都可能需要用到簽到和 統(tǒng)計(jì)功能。簽到后會(huì)給用戶一些禮品以此來吸引用戶持續(xù)在該平臺(tái)進(jìn)行活躍。
    的頭像 發(fā)表于 10-25 16:41 ?562次閱讀