欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

resubmit漸進(jìn)式防重復(fù)提交框架示例

 更新時(shí)間:2022年08月03日 16:25:49   作者:老馬嘯西風(fēng)  
這篇文章主要為大家介紹了resubmit漸進(jìn)式防重復(fù)提交框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

resubmit

resubmit 是一款為 java 設(shè)計(jì)的漸進(jìn)式防止重復(fù)提交框架。

推薦閱讀:

面試官:你們的項(xiàng)目中是怎么做防止重復(fù)提交的?

創(chuàng)作目的

有時(shí)候手動(dòng)加防止重復(fù)提交很麻煩,每次手動(dòng)編寫不利于復(fù)用。

所以希望從從簡到繁實(shí)現(xiàn)一個(gè)工具,便于平時(shí)使用。

特性

  • 漸進(jìn)式實(shí)現(xiàn),可獨(dú)立 spring 使用
  • 基于注解+字節(jié)碼,配置靈活
  • 支持編程式的調(diào)用
  • 支持注解式,完美整合 spring
  • 支持整合 spring-boot

變更日志

maven 引入

<dependency>
    <group>com.github.houbb</group>
    <artifact>resubmit-core</artifact>
    <version>1.0.0</version>
</dependency>

編碼

  • UserService.java

@Resubmit 對(duì)應(yīng)的屬性如下:

屬性說明默認(rèn)值
value()多久內(nèi)禁止重復(fù)提交,單位為毫秒。60000
@Resubmit(5000)
public void queryInfo(final String id) {
    System.out.println("query info: " + id);
}
  • 測試代碼

如果在指定時(shí)間差內(nèi),重復(fù)請(qǐng)求,則會(huì)拋出異常 ResubmitException

@Test(expected = ResubmitException.class)
public void errorTest() {
    UserService service = ResubmitProxy.getProxy(new UserService());
    service.queryInfo("1");
    service.queryInfo("1");
}

相同的參數(shù)直接提交2次,就會(huì)報(bào)錯(cuò)。

  • 測試場景2

如果等待超過指定的 5s,就不會(huì)報(bào)錯(cuò)。

@Test
public void untilTtlTest() {
    UserService service = ResubmitProxy.getProxy(new UserService());
    service.queryInfo("1");
    DateUtil.sleep(TimeUnit.SECONDS, 6);
    service.queryInfo("1");
}

自定義

ResubmitProxy.getProxy(new UserService());

可以獲取 UserService 對(duì)應(yīng)的代理。

等價(jià)于:

ResubmitBs resubmitBs = ResubmitBs.newInstance()
                .cache(new CommonCacheServiceMap())
                .keyGenerator(new KeyGenerator())
                .tokenGenerator(new HttpServletRequestTokenGenerator());
UserService service = ResubmitProxy.getProxy(new UserService(), resubmitBs);

其中 ResubmitBs 作為引導(dǎo)類,對(duì)應(yīng)的策略都支持自定義。

屬性說明默認(rèn)值
cache()緩存實(shí)現(xiàn)策略默認(rèn)為基于 ConcurrentHashMap 實(shí)現(xiàn)的基于內(nèi)存的緩存實(shí)現(xiàn)
keyGenerator()key 實(shí)現(xiàn)策略,用于唯一標(biāo)識(shí)一個(gè)方法+參數(shù),判斷是否為相同的提交md5 策略
tokenGenerator()token 實(shí)現(xiàn)策略,用于唯一標(biāo)識(shí)一個(gè)用戶。從 HttpServletRequest 中的 header 屬性 resubmit_token 中獲取

spring 整合使用

maven 引入

<dependency>
    <group>com.github.houbb</group>
    <artifact>resubmit-spring</artifact>
    <version>1.0.0</version>
</dependency>

代碼編寫

  • UserService.java
@Service
public class UserService {
    @Resubmit(5000)
    public void queryInfo(final String id) {
        System.out.println("query info: " + id);
    }
}
  • SpringConfig.java
@ComponentScan("com.github.houbb.resubmit.test.service")
@EnableResubmit
@Configuration
public class SpringConfig {
}

@EnableResubmit 注解說明

@EnableResubmit 中用戶可以指定對(duì)應(yīng)的實(shí)現(xiàn)策略,便于更加靈活的適應(yīng)業(yè)務(wù)場景。

ResubmitBs 中支持自定義的屬性一一對(duì)應(yīng)。

屬性說明默認(rèn)值
cache()緩存實(shí)現(xiàn)策略默認(rèn)為基于 ConcurrentHashMap 實(shí)現(xiàn)的基于內(nèi)存的緩存實(shí)現(xiàn)
keyGenerator()key 實(shí)現(xiàn)策略,用于唯一標(biāo)識(shí)一個(gè)方法+參數(shù),判斷是否為相同的提交md5 策略
tokenGenerator()token 實(shí)現(xiàn)策略,用于唯一標(biāo)識(shí)一個(gè)用戶。從 HttpServletRequest 中的 header 屬性 resubmit_token 中獲取

測試代碼

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ResubmitSpringTest {
    @Autowired
    private UserService service;
    @Test(expected = ResubmitException.class)
    public void queryTest() {
        service.queryInfo("1");
        service.queryInfo("1");
    }
}

整合 spring-boot

maven 引入

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>resubmit-springboot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

代碼實(shí)現(xiàn)

  • UserService.java

這個(gè)方法實(shí)現(xiàn)和前面的一樣。

@Service
public class UserService {
    @Resubmit(5000)
    public void queryInfo(final String id) {
        System.out.println("query info: " + id);
    }
}
  • Application.java

啟動(dòng)入口

@SpringBootApplication
public class ResubmitApplication {
    public static void main(String[] args) {
        SpringApplication.run(ResubmitApplication.class, args);
    }
}

測試代碼

@ContextConfiguration(classes = ResubmitApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ResubmitSpringBootStarterTest {
    @Autowired
    private UserService service;
    @Test(expected = ResubmitException.class)
    public void queryTest() {
        service.queryInfo("1");
        service.queryInfo("1");
    }
}

自定義策略

上面提到 @EnableResubmit 中的策略支持自定義。

此處僅以 cache 為例,為了簡單,默認(rèn)是基于本地內(nèi)存的緩存實(shí)現(xiàn)。

如果你不是單點(diǎn)應(yīng)用,那么基于 redis 的緩存更加合適

自定義緩存 cache

實(shí)現(xiàn)緩存

只需要實(shí)現(xiàn) ICommonCacheService 接口即可。

public class MyDefineCache extends CommonCacheServiceMap {
    // 這里只是作為演示,實(shí)際生產(chǎn)建議使用 redis 作為統(tǒng)一緩存
    @Override
    public synchronized void set(String key, String value, long expireMills) {
        System.out.println("------------- 自定義的設(shè)置實(shí)現(xiàn)");
        super.set(key, value, expireMills);
    }
}

core 中指定使用

在非 spring 項(xiàng)目中,可以在引導(dǎo)類中指定我們定義的緩存。

ResubmitBs resubmitBs = ResubmitBs.newInstance()
                .cache(new MyDefineCache());
UserService service = ResubmitProxy.getProxy(new UserService(), resubmitBs);

其他使用方式保持不變。

spring 中指定使用

在 spring 項(xiàng)目中,我們需要調(diào)整一下配置,其他不變。

@ComponentScan("com.github.houbb.resubmit.test.service")
@Configuration
@EnableResubmit(cache = "myDefineCache")
public class SpringDefineConfig {
    @Bean("myDefineCache")
    public ICommonCacheService myDefineCache() {
        return new MyDefineCache();
    }
}

@EnableResubmit(cache = "myDefineCache") 指定我們自定義的緩存策略名稱。

Redis 的內(nèi)置緩存策略

為了便于復(fù)用,基于 redis 的緩存策略已實(shí)現(xiàn),后續(xù)有時(shí)間進(jìn)行講解。

開源地址

為了便于大家學(xué)習(xí)使用,目前防重復(fù)提交框架已開源。

歡迎大家 fork+star,鼓勵(lì)一下老馬~

github.com/houbb/resub…

以上就是resubmit漸進(jìn)式防重復(fù)提交框架示例的詳細(xì)內(nèi)容,更多關(guān)于resubmit防重復(fù)提交的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論