溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊

發(fā)布時(shí)間:2022-03-21 09:13:42 來源:億速云 閱讀:202 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何使用Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

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

/**
     *
     * @param userId 點(diǎn)贊的人
     * @param type 點(diǎn)贊與取消點(diǎn)贊的表示
     * @param textId   文章ID
     * @param entityUserId -- 被點(diǎn)贊的人,文章作者
     */
    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);
                //多個(gè)更新操作,需要事務(wù)
                operations.multi();
                if (isMember) {
                    //取消贊
                    redisTemplate.opsForSet().remove(entityLikeKey, userId);
                    redisTemplate.opsForValue().decrement(userLikeKey);
                } else {
                    //點(diǎn)贊
                    redisTemplate.opsForSet().add(entityLikeKey, userId);
                    redisTemplate.opsForValue().increment(userLikeKey);
                }
                return operations.exec();
            }
        });

    }

    /**
     *查詢某實(shí)體(帖子,評(píng)論等)點(diǎn)贊數(shù)量
     * @param type 1點(diǎn)贊,2評(píng)論。0表示取消點(diǎn)贊
     * @param textId
     * @return
     */
    private long findEntityLikeCount(int type, int textId){
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(type, textId);
        return redisTemplate.opsForSet().size(entityLikeKey);
    }

    /**
     * 查詢某人對(duì)某文章的點(diǎn)贊狀態(tài)
     * @param textId 帖子ID
     * @param userId
     * @return
     */
    private int findEntityLikeStatus(int textId,long userId){
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(1, textId);
        //此處返回int,是為了進(jìn)行擴(kuò)展。比如擴(kuò)展踩,為止2.等等情況
        return redisTemplate.opsForSet().isMember(entityLikeKey,userId)?1:0;
    }

    /**
     * 查詢某個(gè)用戶獲得贊,用于在個(gè)人主頁(yè)查看收獲了多少贊
     * @param userId
     * @return
     */
    private int findUserLikeCount(long userId){
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        // count.intValue()數(shù)據(jù)的整數(shù)形式;
        return count==null?0:count.intValue();
    }

Redis–key設(shè)置

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";
    /**
     *某個(gè)實(shí)體收到的贊,如帖子,
     * like:entity:entityType:entityId -> set(userId) 對(duì)應(yīng)set,存入userId
     * @param entityType
     * @param entityId
     * @return
     */
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + entityType + SPLIT + entityId;
    }
     *某個(gè)用戶收到的總贊數(shù)
     * like:user:userId ->long
     * @param userId
    public static String getUserLikeKey(long userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
     * 匯總某個(gè)帖子的評(píng)論數(shù)量
    public static String getUserCommentsKey(int articleId) {
        return PREFIX_USER_COMMENTS + SPLIT + articleId;

關(guān)于“如何使用Redis實(shí)現(xiàn)點(diǎn)贊取消點(diǎn)贊”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI