亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

redis防止重復提交的實現(xiàn)示例

 更新時間:2024年06月06日 11:28:46   作者:MurphyGuan  
在開發(fā)中我們都需要處理重復提交的問題,本文主要介紹了redis防止重復提交的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下

說到防止重復提交,解決方案有很多。比如使用token、redis、表字段唯一約束、時間戳判斷等。本篇文章介紹一下redis防止重復提交。

第一次遇到這個問題,是在我們的app點贊功能發(fā)現(xiàn)的。產(chǎn)品要求點贊只能點一次,但是發(fā)現(xiàn)快速多次點贊也是可以成功的。因為客戶端在第一次請求接口后,還沒有來得及把點贊圖標置灰,就發(fā)起了第二次請求。

最后這個問題的解決方案是利用redis原子特性解決,在接口請求后設置一個短有效期的緩存,下次接口請求時繼續(xù)設置這個緩存,如果緩存沒有設置成功則代表重復提交。

下面說一下具體實現(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緩存操作實現(xiàn)

在這里我們使用RedisTemplate的setIfAbsent方法設置了緩存并設置了一個10s的有效期,具體過期時間可以根據(jù)業(yè)務來定。setIfAbsent方法的意思是如果緩存之前不存在,設置成功后返回true。如果緩存之前存在了則不進行設置并且返回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操作實現(xiàn)
 * @author murphy
 * @date 2024/6/6
 */
@Service
public class RedisService {

    private static final int TTL = 10; // 過期時間,單位是秒

    @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é)果來判斷是否屬于重復提交。下面模擬多次請求:

@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é)果:

到此這篇關于redis防止重復提交的實現(xiàn)示例的文章就介紹到這了,更多相關redis防止重復提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家! 

相關文章

最新評論