目錄
程式碼實作:
首頁 資料庫 Redis 如何使用Redis實現按讚取消按讚

如何使用Redis實現按讚取消按讚

May 27, 2023 pm 03:57 PM
redis

程式碼實作:

/**
     *
     * @param userId 点赞的人
     * @param type 点赞与取消点赞的表示
     * @param textId   文章ID
     * @param entityUserId -- 被点赞的人,文章作者
     */
    private void like(long userId,int type,int textId,long entityUserId){
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId);
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);
                boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);
                //多个更新操作,需要事务
                operations.multi();
                if (isMember) {
                    //取消赞
                    redisTemplate.opsForSet().remove(entityLikeKey, userId);
                    redisTemplate.opsForValue().decrement(userLikeKey);
                } else {
                    //点赞
                    redisTemplate.opsForSet().add(entityLikeKey, userId);
                    redisTemplate.opsForValue().increment(userLikeKey);
                }
                return operations.exec();
            }
        });

    }

    /**
     *查询某实体(帖子,评论等)点赞数量
     * @param type 1点赞,2评论。0表示取消点赞
     * @param textId
     * @return
     */
    private long findEntityLikeCount(int type, int textId){
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId);
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    /**
     * 查询某人对某文章的点赞状态
     * @param textId 帖子ID
     * @param userId
     * @return
     */
    private int findEntityLikeStatus(int textId,long userId){
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(1, textId);
        //此处返回int,是为了进行扩展。比如扩展踩,为止2.等等情况
        return redisTemplate.opsForSet().isMember(entityLikeKey,userId)?1:0;
    }

    /**
     * 查询某个用户获得赞,用于在个人主页查看收获了多少赞
     * @param userId
     * @return
     */
    private int findUserLikeCount(long userId){
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        // count.intValue()数据的整数形式;
        return count==null?0:count.intValue();
    }
登入後複製

Redis–key設定

public class RedisKeyUtil {
    private static final String SPLIT = ":";
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    private static final String PREFIX_USER_LIKE = "like:user";
    private static final String PREFIX_USER_COMMENTS="comments:user";
    /**
     *某个实体收到的赞,如帖子,
     * like:entity:entityType:entityId -> set(userId) 对应set,存入userId
     * @param entityType
     * @param entityId
     * @return
     */
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId;
    }
     *某个用户收到的总赞数
     * like:user:userId ->long
     * @param userId
    public static String getUserLikeKey(long userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
     * 汇总某个帖子的评论数量
    public static String getUserCommentsKey(int articleId) {
        return PREFIX_USER_COMMENTS + SPLIT + articleId;
登入後複製

以上是如何使用Redis實現按讚取消按讚的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法 Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法 May 08, 2024 pm 03:50 PM

Windows11安裝10.0.22000.100跳出0x80242008錯誤解決方法

剖析 PHP 函數瓶頸,提升執行效率 剖析 PHP 函數瓶頸,提升執行效率 Apr 23, 2024 pm 03:42 PM

剖析 PHP 函數瓶頸,提升執行效率

Golang API快取策略與最佳化 Golang API快取策略與最佳化 May 07, 2024 pm 02:12 PM

Golang API快取策略與最佳化

redis是記憶體快取嗎 redis是記憶體快取嗎 Apr 20, 2024 am 05:26 AM

redis是記憶體快取嗎

redis是非關係型資料庫嗎 redis是非關係型資料庫嗎 Apr 20, 2024 am 05:36 AM

redis是非關係型資料庫嗎

erlang和golang性能哪個好? erlang和golang性能哪個好? Apr 21, 2024 am 03:24 AM

erlang和golang性能哪個好?

PHP開發中的快取機制與應用實戰 PHP開發中的快取機制與應用實戰 May 09, 2024 pm 01:30 PM

PHP開發中的快取機制與應用實戰

PHP數組分頁中如何使用Redis快取? PHP數組分頁中如何使用Redis快取? May 01, 2024 am 10:48 AM

PHP數組分頁中如何使用Redis快取?

See all articles