最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

redis實(shí)現(xiàn)存儲帖子的點(diǎn)贊狀態(tài)和數(shù)量的示例代碼

 更新時(shí)間:2023年09月06日 10:54:58   作者:退役熬夜冠軍選手  
使用Redis來實(shí)現(xiàn)點(diǎn)贊功能是一種高效的選擇,因?yàn)镽edis是一個(gè)內(nèi)存數(shù)據(jù)庫,適用于處理高并發(fā)的數(shù)據(jù)操作,這篇文章主要介紹了redis實(shí)現(xiàn)存儲帖子的點(diǎn)贊狀態(tài)和數(shù)量的示例代碼,需要的朋友可以參考下

redis實(shí)現(xiàn)存儲帖子的點(diǎn)贊狀態(tài)和數(shù)量

1 對redis進(jìn)行配置并封裝一個(gè)redis工具類

@Configuration //編寫redis的配置類
public class RedisConfig {
    @Bean //參數(shù)聲明了連接工廠
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 設(shè)置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // 設(shè)置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        // 設(shè)置hash的key的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // 設(shè)置hash的value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
           //讓設(shè)置生效
        template.afterPropertiesSet();
        return template;
    }
}
public class RedisKeyUtil {
    private static final String SPLIT = ":";
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    // 某個(gè)實(shí)體的贊
    // like:entity:entityType:entityId -> set(userId)
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }
    }

2 reids操作起來比較簡單,所以一般不需要寫dao層,直接在service里面對數(shù)據(jù)進(jìn)行操作

@Service
public class LikeService {
    @Autowired
    private RedisTemplate redisTemplate;
    // 點(diǎn)贊  誰點(diǎn)的贊 點(diǎn)贊的實(shí)體 實(shí)體的id 實(shí)體的用戶
    public void like(int userId, int entityType, int entityId, ) {
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);
                   //是否已經(jīng)點(diǎn)過贊
                if (isMember) { //移除userid
                    operations.opsForSet().remove(entityLikeKey, userId);
                    operations.opsForValue().decrement(userLikeKey);
                } else { //添加userid
                    operations.opsForSet().add(entityLikeKey, userId);
                    operations.opsForValue().increment(userLikeKey);
                }
            }
        });
    }
    // 查詢某實(shí)體點(diǎn)贊的數(shù)量
    public long findEntityLikeCount(int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        return redisTemplate.opsForSet().size(entityLikeKey);
    }
    // 查詢某人對某實(shí)體的點(diǎn)贊狀態(tài)
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        //1表示點(diǎn)贊 0表示沒有
        return redisTemplate.opsForSet().isMember(entityLikeKey, userId) ? 1 : 0;
    }

3controller層接受post請求帶來的參數(shù),將查詢到的數(shù)據(jù)放進(jìn)map里,傳給前端回調(diào)函數(shù),處理前端頁面

@Controller
public class LikeController {
    @Autowired
    private LikeService likeService;
    @Autowired
    private HostHolder hostHolder;
    @RequestMapping(path = "/like", method = RequestMethod.POST)
    @ResponseBody
    public String like(int entityType, int entityId, int entityUserId) {
        User user = hostHolder.getUser();
        // 點(diǎn)贊
        likeService.like(user.getId(), entityType, entityId, entityUserId);
        // 數(shù)量
        long likeCount = likeService.findEntityLikeCount(entityType, entityId);
        // 狀態(tài)
        int likeStatus = likeService.findEntityLikeStatus(user.getId(), entityType, entityId);
        // 返回的結(jié)果
        Map<String, Object> map = new HashMap<>();
        map.put("likeCount", likeCount);
        map.put("likeStatus", likeStatus);
        return CommunityUtil.getJSONString(0, null, map);
    }
}
//回調(diào)函數(shù)
function like(btn, entityType, entityId, entityUserId) {
    $.post(
        CONTEXT_PATH + "/like",
        {"entityType":entityType,"entityId":entityId,"entityUserId":entityUserId},
        function(data) {
            data = $.parseJSON(data);
            if(data.code == 0) {
                $(btn).children("i").text(data.likeCount);
                $(btn).children("b").text(data.likeStatus==1?'已贊':"贊");
            } else {
                alert(data.msg);
            }
        }
    );
}

使用Redis來實(shí)現(xiàn)點(diǎn)贊功能的基本思路

使用Redis來實(shí)現(xiàn)點(diǎn)贊功能是一種高效的選擇,因?yàn)镽edis是一個(gè)內(nèi)存數(shù)據(jù)庫,適用于處理高并發(fā)的數(shù)據(jù)操作。以下是一個(gè)基本的點(diǎn)贊功能在Redis中的設(shè)計(jì)示例:

假設(shè)我們有一個(gè)文章或帖子,用戶可以對其進(jìn)行點(diǎn)贊,取消點(diǎn)贊,以及查看點(diǎn)贊總數(shù)。

存儲點(diǎn)贊信息:

使用Redis的Hash數(shù)據(jù)結(jié)構(gòu)來存儲每篇文章的點(diǎn)贊信息。每篇文章對應(yīng)一個(gè)Hash,Hash的字段表示用戶ID,字段值表示點(diǎn)贊狀態(tài)(例如1代表已點(diǎn)贊,0代表未點(diǎn)贊)(值也可以存放用戶點(diǎn)贊時(shí)間)。

HSET article_likes:<article_id> <user_id> 1

記錄點(diǎn)贊總數(shù):

使用 Redis 的 Set 數(shù)據(jù)結(jié)構(gòu)來存儲每篇文章的點(diǎn)贊用戶集合,用于查詢文章的點(diǎn)贊數(shù)量。每篇文章對應(yīng)一個(gè) Set,其中的元素為用戶ID。

SADD article_likes_count:<article_id> <user_id>

取消點(diǎn)贊:

取消點(diǎn)贊時(shí),從Hash中刪除用戶的點(diǎn)贊記錄,同時(shí)從對應(yīng)的 Set 中刪除用戶ID。

HDEL article_likes:<article_id> <user_id>
SREM article_likes_count:<article_id> <user_id>

查詢點(diǎn)贊狀態(tài):

查詢某篇文章的點(diǎn)贊狀態(tài),只需要查詢對應(yīng)的Hash數(shù)據(jù)結(jié)構(gòu)中的字段值。

HGET article_likes:<article_id> <user_id>

查詢點(diǎn)贊總數(shù):

查詢某篇文章的點(diǎn)贊總數(shù),使用Redis的PFCount命令來統(tǒng)計(jì)HyperLogLog的數(shù)量。

SCARD article_likes_count:<article_id>

這只是一個(gè)簡單的Redis設(shè)計(jì)示例。在實(shí)際應(yīng)用中,您可能還需要考慮數(shù)據(jù)過期策略、持久化選項(xiàng)、緩存更新機(jī)制等等。另外,確保對Redis進(jìn)行適當(dāng)?shù)呐渲煤蛢?yōu)化,以滿足您應(yīng)用的性能和可靠性需求。

到此這篇關(guān)于redis實(shí)現(xiàn)存儲帖子的點(diǎn)贊狀態(tài)和數(shù)量的文章就介紹到這了,更多相關(guān)redis點(diǎn)贊狀態(tài)和數(shù)量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis中刪除策略的幾種實(shí)現(xiàn)方式

    Redis中刪除策略的幾種實(shí)現(xiàn)方式

    本文詳細(xì)介紹了Redis的過期鍵刪除策略和內(nèi)存淘汰策略,過期鍵刪除策略包括定時(shí)刪除、惰性刪除和定期刪除,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-11-11
  • Redis 實(shí)現(xiàn)“附近的人”功能

    Redis 實(shí)現(xiàn)“附近的人”功能

    Redis基于geohash和有序集合提供了地理位置相關(guān)功能。這篇文章主要介紹了Redis 實(shí)現(xiàn)“附近的人”功能,需要的朋友可以參考下
    2019-11-11
  • 解析Redis的緩存類型

    解析Redis的緩存類型

    本文主要介紹了Redis的緩存類型,主要介紹了4種緩存,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Redis大Key(Bigkey)問題識別與解決全解析

    Redis大Key(Bigkey)問題識別與解決全解析

    在高并發(fā)場景下,Redis 以極致的內(nèi)存操作速度成為緩存與NoSQL領(lǐng)域的首選,但隨著業(yè)務(wù)發(fā)展,大Key(Bigkey) 問題逐漸顯現(xiàn),帶來內(nèi)存風(fēng)險(xiǎn)、性能瓶頸、集群失衡等隱患,本文給大家介紹了Redis大Key(Bigkey)問題識別與解決,需要的朋友可以參考下
    2025-08-08
  • Redis中有序集合的內(nèi)部實(shí)現(xiàn)方式的詳細(xì)介紹

    Redis中有序集合的內(nèi)部實(shí)現(xiàn)方式的詳細(xì)介紹

    本文主要介紹了Redis中有序集合的內(nèi)部實(shí)現(xiàn)方式的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Redis中List類型的常用命令

    Redis中List類型的常用命令

    本文主要介紹了Redis中List類型的常用命令,包含12種常用命令,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • 詳解Redis BoundValueOperations使用及實(shí)現(xiàn)

    詳解Redis BoundValueOperations使用及實(shí)現(xiàn)

    BoundValueOperations是Spring Data Redis中一個(gè)非常實(shí)用的工具,本文主要介紹了Redis BoundValueOperations使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-09-09
  • Redis 有序集合的使用場景

    Redis 有序集合的使用場景

    在Redis的學(xué)習(xí)中,有序集合是一種非常實(shí)用的數(shù)據(jù)結(jié)構(gòu),本文就來介紹一下Redis 有序集合的使用場景,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • 在Redis集群中使用pipeline批量插入的實(shí)現(xiàn)方法

    在Redis集群中使用pipeline批量插入的實(shí)現(xiàn)方法

    這篇文章主要介紹了在Redis集群中使用pipeline批量插入的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • spring?boot集成redis基礎(chǔ)入門實(shí)例詳解

    spring?boot集成redis基礎(chǔ)入門實(shí)例詳解

    redis在spring?boot項(xiàng)目開發(fā)中是常用的緩存套件,常見使用的是spring-boot-starter-data-redis,這篇文章主要介紹了spring?boot集成redis基礎(chǔ)入門,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10

最新評論

临泽县| 弋阳县| 嵩明县| 五常市| 舞阳县| 凤庆县| 永年县| 海兴县| 巴彦县| 密山市| 四子王旗| 永定县| 桃源县| 利津县| 胶南市| 乌海市| 民勤县| 凭祥市| 崇左市| 邵武市| 永年县| 陵川县| 霍城县| 嵩明县| 泗水县| 太保市| 上饶县| 阿拉善盟| 张家口市| 南岸区| 普兰店市| 阳泉市| 临沭县| 寻甸| 金乡县| 衡水市| 闽清县| 施秉县| 紫云| 德州市| 盐源县|