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

Redis如何實現(xiàn)投票功能

 更新時間:2024年05月06日 16:08:51   作者:沙漠真有魚  
這篇文章主要介紹了Redis如何實現(xiàn)投票功能,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、背景介紹

投票功能是一個非常常見的Web應(yīng)用場景,SpringBoot作為當(dāng)今流行的Web開發(fā)框架,為了提高開發(fā)效率和性能,通常需要整合一些第三方組件。

Redis是一種高性能的鍵值對存儲數(shù)據(jù)庫,而Mybatis-plus則是Mybatis的擴展版本,提供了更強大和便捷的數(shù)據(jù)庫操作方式。

本文將介紹如何將Redis和Mybatis-plus整合到SpringBoot中,實現(xiàn)投票功能。

二、開發(fā)環(huán)境

  • JDK 1.8
  • SpringBoot 2.5.0
  • Redis 6.2.4
  • Mybatis-plus 3.4.3
  • IntelliJ IDEA

三、技術(shù)實現(xiàn)

1. 配置Redis

在SpringBoot的配置文件application.yml中添加Redis的配置:

spring:
  # Redis相關(guān)配置
  redis:
    # Redis服務(wù)器IP地址
    host: localhost
    # Redis服務(wù)器端口號
    port: 6379
    # Redis服務(wù)器密碼
    password: 
    # Redis連接池最大連接數(shù)
    jedis:
      pool:
        max-active: 8
    # Redis連接池最大等待時間(單位:毫秒)
    lettuce:
      pool:
        max-wait: -1ms
    timeout: 5000ms

2. 配置Mybatis-plus

在SpringBoot的配置類中添加Mybatis-plus的配置:

@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {

    /**
     * Mybatis-plus分頁插件配置
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * Mybatis-plus通用Mapper配置
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        scannerConfigurer.setBasePackage("com.example.mapper");
        return scannerConfigurer;
    }
}

3. 實現(xiàn)投票功能

首先創(chuàng)建一個投票的實體類Vote,包含投票項的id和投票數(shù)count:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Vote implements Serializable {
    private Long id;
    private Integer count;
}

然后創(chuàng)建投票的數(shù)據(jù)庫表vote,包含兩個字段id和count,id為主鍵:

CREATE TABLE `vote` (
  `id` bigint(20) NOT NULL,
  `count` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

接著創(chuàng)建投票的Mapper接口VoteMapper和對應(yīng)的XML文件VoteMapper.xml,定義增加投票數(shù)和查詢投票數(shù)的方法:

public interface VoteMapper extends BaseMapper<Vote> {
    /**
     * 增加投票數(shù)
     * @param id 投票項id
     * @return
     */
    int increaseCount(@Param("id") Long id);

    /**
     * 查詢投票數(shù)
     * @param id 投票項id
     * @return
     */
    int selectCount(@Param("id") Long id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.VoteMapper">

    <!-- 增加投票數(shù) -->
    <update id="increaseCount">
        update vote set count = count + 1
        where id = #{id}
    </update>

    <!-- 查詢投票數(shù) -->
    <select id="selectCount" resultType="int">
        select count
        from vote
        where id = #{id}
    </select>

</mapper>

接下來創(chuàng)建投票的Service類VoteService,其中增加投票數(shù)和查詢投票數(shù)的方法使用了Redis緩存:

@Service
public class VoteService {

    @Autowired
    private VoteMapper voteMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     ** @param id 投票項id
     */
    public void increaseCount(Long id) {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        String key = "vote:" + id;
        // 先從緩存中獲取投票數(shù)
        Integer count = (Integer) operations.get(key);
        // 如果緩存中沒有,則從數(shù)據(jù)庫中獲取,并存入緩存
        if (count == null) {
            count = voteMapper.selectCount(id);
            if (count != null) {
                operations.set(key, count);
            }
        }
        // 如果緩存中有,則增加投票數(shù)并更新緩存
        if (count != null) {
            operations.increment(key);
            voteMapper.increaseCount(id);
        }
    }

    /**
     * 查詢投票數(shù)
     * @param id 投票項id
     * @return
     */
    public Integer selectCount(Long id) {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        String key = "vote:" + id;
        // 先從緩存中獲取投票數(shù)
        Integer count = (Integer) operations.get(key);
        // 如果緩存中沒有,則從數(shù)據(jù)庫中獲取,并存入緩存
        if (count == null) {
            count = voteMapper.selectCount(id);
            if (count != null) {
                operations.set(key, count);
            }
        }
        return count;
    }
}

最后創(chuàng)建投票的Controller類VoteController,提供增加投票數(shù)和查詢投票數(shù)的接口:

@RestController
public class VoteController {

    @Autowired
    private VoteService voteService;

    /**
     * 增加投票數(shù)接口
     * @param id 投票項id
     * @return
     */
    @PostMapping("/vote/increase")
    public String increaseCount(@RequestParam Long id) {
        voteService.increaseCount(id);
        return "success";
    }

    /**
     * 查詢投票數(shù)接口
     * @param id 投票項id
     * @return
     */
    @GetMapping("/vote/select")
    public Integer selectCount(@RequestParam Long id) {
        Integer count = voteService.selectCount(id);
        return count == null ? 0 : count;
    }
}

四、測試運行

啟動SpringBoot應(yīng)用后,在瀏覽器中訪問http://localhost:8080/vote/select?id=1,可以查詢id為1的投票項的投票數(shù);

再訪問http://localhost:8080/vote/increase?id=1,可以對id為1的投票項進行投票。

同時可以在Redis客戶端中查看投票項的投票數(shù)是否正確。

五、總結(jié)

本文介紹了如何將Redis和Mybatis-plus整合到SpringBoot中,以實現(xiàn)投票功能。

其中Redis緩存可以增加應(yīng)用性能,Mybatis-plus可以簡化數(shù)據(jù)庫操作。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題

    關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題

    針對上面問題,一般的解決方案是使用分布式鎖來解決,本文通過場景分析給大家介紹關(guān)于SpringBoot 使用 Redis 分布式鎖解決并發(fā)問題,感興趣的朋友一起看看吧
    2021-11-11
  • 關(guān)于Redis緩存問題及解決

    關(guān)于Redis緩存問題及解決

    這篇文章主要介紹了關(guān)于Redis緩存問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Redis的幾種數(shù)據(jù)類型使用詳解

    Redis的幾種數(shù)據(jù)類型使用詳解

    這篇文章主要介紹了Redis的幾種數(shù)據(jù)類型使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • React實現(xiàn)組件之間通信的幾種常用方法

    React實現(xiàn)組件之間通信的幾種常用方法

    在?React?中,組件之間的通信是構(gòu)建復(fù)雜應(yīng)用程序的核心部分,良好的組件間通信能夠提高代碼的可維護性和可讀性,同時能夠高效地管理應(yīng)用狀態(tài),在這篇博客中,我們將探討?React中幾種常用的組件通信方法,并提供示例代碼來幫助你理解,需要的朋友可以參考下
    2025-02-02
  • Redis分布式鎖升級版RedLock及SpringBoot實現(xiàn)方法

    Redis分布式鎖升級版RedLock及SpringBoot實現(xiàn)方法

    這篇文章主要介紹了Redis分布式鎖升級版RedLock及SpringBoot實現(xiàn),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Redis緩存異常之緩存雪崩問題解讀

    Redis緩存異常之緩存雪崩問題解讀

    文章主要介紹了緩存雪崩、擊穿和穿透問題,以及針對這些問題的解決方法,包括服務(wù)熔斷、服務(wù)降級、請求限流和布隆過濾器等
    2025-01-01
  • Redis集群的相關(guān)詳解

    Redis集群的相關(guān)詳解

    這篇文章主要介紹了Redis集群的相關(guān),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • redis隊列和秒殺應(yīng)用方式

    redis隊列和秒殺應(yīng)用方式

    這篇文章主要介紹了redis隊列和秒殺應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Redis兩種持久化方案RDB和AOF詳解

    Redis兩種持久化方案RDB和AOF詳解

    這篇文章主要介紹了Redis 兩種持久化方案,RDB(Redis DataBase)和 AOF(Append Only File),給大家提供參考,一起學(xué)習(xí)下。
    2017-11-11
  • 基于redis 7.2.3的makefile源碼解讀學(xué)習(xí)

    基于redis 7.2.3的makefile源碼解讀學(xué)習(xí)

    這篇文章主要為大家介紹了基于redis 7.2.3的makefile源碼解讀學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12

最新評論