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

redis實(shí)現(xiàn)存儲(chǔ)帖子的點(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)存儲(chǔ)帖子的點(diǎn)贊狀態(tài)和數(shù)量的示例代碼,需要的朋友可以參考下

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

1 對(duì)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里面對(duì)數(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);
    }
    // 查詢某人對(duì)某實(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請(qǐng)求帶來的參數(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è)文章或帖子,用戶可以對(duì)其進(jìn)行點(diǎn)贊,取消點(diǎn)贊,以及查看點(diǎn)贊總數(shù)。

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

使用Redis的Hash數(shù)據(jù)結(jié)構(gòu)來存儲(chǔ)每篇文章的點(diǎn)贊信息。每篇文章對(duì)應(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)來存儲(chǔ)每篇文章的點(diǎn)贊用戶集合,用于查詢文章的點(diǎn)贊數(shù)量。每篇文章對(duì)應(yīng)一個(gè) Set,其中的元素為用戶ID。

SADD article_likes_count:<article_id> <user_id>

取消點(diǎn)贊:

取消點(diǎn)贊時(shí),從Hash中刪除用戶的點(diǎn)贊記錄,同時(shí)從對(duì)應(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),只需要查詢對(duì)應(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ī)制等等。另外,確保對(duì)Redis進(jìn)行適當(dāng)?shù)呐渲煤蛢?yōu)化,以滿足您應(yīng)用的性能和可靠性需求。

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

相關(guān)文章

  • redis發(fā)布訂閱模式的實(shí)現(xiàn)

    redis發(fā)布訂閱模式的實(shí)現(xiàn)

    本文主要介紹了redis發(fā)布訂閱模式,Redis?的?SUBSCRIBE?命令可以讓客戶端訂閱任意數(shù)量的頻道,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • Redis和springboot 整合redisUtil類的示例代碼

    Redis和springboot 整合redisUtil類的示例代碼

    這篇文章主要介紹了Redis和springboot 整合redisUtil類的示例代碼,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Redisson如何解決redis分布式鎖過期時(shí)間到了業(yè)務(wù)沒執(zhí)行完問題

    Redisson如何解決redis分布式鎖過期時(shí)間到了業(yè)務(wù)沒執(zhí)行完問題

    這篇文章主要介紹了Redisson如何解決redis分布式鎖過期時(shí)間到了業(yè)務(wù)沒執(zhí)行完問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 提高redis緩存命中率的方法

    提高redis緩存命中率的方法

    在本篇文章里小編給大家整理了關(guān)于怎么提高redis緩存命中率的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們跟著學(xué)習(xí)下。
    2019-06-06
  • Redis內(nèi)存回收策略

    Redis內(nèi)存回收策略

    Redis也會(huì)因?yàn)閮?nèi)存不足而產(chǎn)生錯(cuò)誤?,?也可能因?yàn)榛厥者^久而導(dǎo)致系統(tǒng)長期的停頓,因此掌握?qǐng)?zhí)行回收策略十分有必要,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-11-11
  • 使用Redis實(shí)現(xiàn)用戶積分排行榜的教程

    使用Redis實(shí)現(xiàn)用戶積分排行榜的教程

    這篇文章主要介紹了使用Redis實(shí)現(xiàn)用戶積分排行榜的教程,包括一個(gè)用PHP腳本進(jìn)行操作的例子,需要的朋友可以參考下
    2015-04-04
  • Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查

    Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查

    這篇文章主要介紹了Redis在Ubuntu系統(tǒng)上無法啟動(dòng)的問題排查,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • redis服務(wù)啟動(dòng)與停止方式

    redis服務(wù)啟動(dòng)與停止方式

    這篇文章主要介紹了redis服務(wù)啟動(dòng)與停止方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Redis中List類型的常用命令

    Redis中List類型的常用命令

    本文主要介紹了Redis中List類型的常用命令,包含12種常用命令,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • Redis Cluster集群動(dòng)態(tài)擴(kuò)容的實(shí)現(xiàn)

    Redis Cluster集群動(dòng)態(tài)擴(kuò)容的實(shí)現(xiàn)

    本文主要介紹了Redis Cluster集群動(dòng)態(tài)擴(kuò)容的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評(píng)論