redis防止重復(fù)提交的實(shí)現(xiàn)示例
說到防止重復(fù)提交,解決方案有很多。比如使用token、redis、表字段唯一約束、時(shí)間戳判斷等。本篇文章介紹一下redis防止重復(fù)提交。
第一次遇到這個(gè)問題,是在我們的app點(diǎn)贊功能發(fā)現(xiàn)的。產(chǎn)品要求點(diǎn)贊只能點(diǎn)一次,但是發(fā)現(xiàn)快速多次點(diǎn)贊也是可以成功的。因?yàn)榭蛻舳嗽诘谝淮握埱蠼涌诤螅€沒有來得及把點(diǎn)贊圖標(biāo)置灰,就發(fā)起了第二次請求。
最后這個(gè)問題的解決方案是利用redis原子特性解決,在接口請求后設(shè)置一個(gè)短有效期的緩存,下次接口請求時(shí)繼續(xù)設(shè)置這個(gè)緩存,如果緩存沒有設(shè)置成功則代表重復(fù)提交。
下面說一下具體實(shí)現(xiàn):
1.redis依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.redis配置
package com.example.redis.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置類 * @author murphy * @date 2024/6/6 */ @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } }
3.application.yml配置redis連接信息
spring: redis: host: 127.0.0.1 port: 6379 database: 0
4.redis緩存操作實(shí)現(xiàn)
在這里我們使用RedisTemplate的setIfAbsent方法設(shè)置了緩存并設(shè)置了一個(gè)10s的有效期,具體過期時(shí)間可以根據(jù)業(yè)務(wù)來定。setIfAbsent方法的意思是如果緩存之前不存在,設(shè)置成功后返回true。如果緩存之前存在了則不進(jìn)行設(shè)置并且返回false。
package com.example.redis.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; /** * redis操作實(shí)現(xiàn) * @author murphy * @date 2024/6/6 */ @Service public class RedisService { private static final int TTL = 10; // 過期時(shí)間,單位是秒 @Autowired private RedisTemplate<String, Object> redisTemplate; public boolean preventDuplicateSubmission(String userId) { String key = "praise_:" + userId; Boolean success = redisTemplate.opsForValue().setIfAbsent(key, "1", TTL, TimeUnit.SECONDS); return Boolean.TRUE.equals(success); } }
5.測試
我們可以根據(jù)preventDuplicateSubmission方法的返回結(jié)果來判斷是否屬于重復(fù)提交。下面模擬多次請求:
@Test void contextLoads() { boolean res1 = redisService.preventDuplicateSubmission("1"); System.out.println("第一次提交結(jié)果:" + res1); boolean res2 = redisService.preventDuplicateSubmission("1"); System.out.println("第二次提交結(jié)果:" + res2); boolean res3 = redisService.preventDuplicateSubmission("1"); System.out.println("第三次提交結(jié)果:" + res3); }
返回結(jié)果:
到此這篇關(guān)于redis防止重復(fù)提交的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)redis防止重復(fù)提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot使用Hibernate-Validator校驗(yàn)參數(shù)時(shí)的長度校驗(yàn)方法詳解
這篇文章主要給大家介紹了關(guān)于Spring?Boot使用Hibernate-Validator校驗(yàn)參數(shù)時(shí)的長度校驗(yàn)方法的相關(guān)資料,在Spring Boot中可以使用Spring Boot Validation來對參數(shù)名稱進(jìn)行校驗(yàn),需要的朋友可以參考下2023-08-08關(guān)于java入門與java開發(fā)環(huán)境配置詳細(xì)教程
這篇文章主要介紹了關(guān)于java入門與java開發(fā)環(huán)境配置詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03java.lang.NumberFormatException異常解決方案詳解
這篇文章主要介紹了java.lang.NumberFormatException異常解決方案詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08java實(shí)現(xiàn)簡單解析XML文件功能示例
這篇文章主要介紹了java實(shí)現(xiàn)簡單解析XML文件功能,結(jié)合實(shí)例形式分析了java針對xml文件的讀取、遍歷節(jié)點(diǎn)及輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10一文快速了解spring?boot中的@idempotent注解
idempotence注解是RESTful API設(shè)計(jì)中一個(gè)重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關(guān)于spring?boot中@idempotent注解的相關(guān)資料,需要的朋友可以參考下2024-01-01SpringBoot Starter自定義之創(chuàng)建可復(fù)用的自動(dòng)配置模塊方式
本文將詳細(xì)介紹如何設(shè)計(jì)和實(shí)現(xiàn)一個(gè)自定義的Spring Boot Starter,幫助讀者掌握這一強(qiáng)大技術(shù),提升代碼復(fù)用性和開發(fā)效率,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04Kotlin常用函數(shù)let,with,run,apply用法與區(qū)別案例詳解
這篇文章主要介紹了Kotlin常用函數(shù)let,with,run,apply用法與區(qū)別案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09mapstruct的用法之qualifiedByName示例詳解
qualifiedByName的意思就是使用這個(gè)Mapper接口中的指定的默認(rèn)方法去處理這個(gè)屬性的轉(zhuǎn)換,而不是簡單的get?set,今天通過本文給大家介紹下mapstruct的用法之qualifiedByName示例詳解,感興趣的朋友一起看看吧2022-04-04Java中字符序列的替換與分解的幾種實(shí)現(xiàn)方法
本文主要介紹了Java中字符序列的替換與分解的幾種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02