聊天服务器——缓存器

CacheManager

缓存管理器里存放的是通知消息和聊天消息的缓存。因为用户会不在线,因此当用户不在线的时候,有朋友发消息或者群消息的时候,这些消息没办法传送给用户。因此先把消息传送到缓存器上,当用户上线的时候,会从缓存器上把缓存的消息推送给上线的用户。

代码

通知消息缓存

struct NotifyMsgCache
{
    int32_t     userid;
    std::string notifymsg;  
};

聊天消息缓存

struct ChatMsgCache
{
    int32_t     userid;
    std::string chatmsg;
};

两个缓存都是用户id和string进行配对

缓存器

通知消息和聊天消息都是采用list来存放。

功能函数主要有添加通知消息缓存和获取通知消息缓存

添加聊天消息缓存和获取聊天消息缓存。

class MsgCacheManager final
{
public:
    MsgCacheManager();
    ~MsgCacheManager();
​
    MsgCacheManager(const MsgCacheManager& rhs) = delete;
    MsgCacheManager& operator =(const MsgCacheManager& rhs) = delete;
​
    bool AddNotifyMsgCache(int32_t userid, const std::string& cache);
    void GetNotifyMsgCache(int32_t userid, std::list<NotifyMsgCache>& cached);
​
    bool AddChatMsgCache(int32_t userid, const std::string& cache);
    void GetChatMsgCache(int32_t userid, std::list<ChatMsgCache>& cached);
​
​
private:
    std::list<NotifyMsgCache>       m_listNotifyMsgCache;    //通知类消息缓存,比如加好友消息
    std::mutex                      m_mtNotifyMsgCache;
    std::list<ChatMsgCache>         m_listChatMsgCache;      //聊天消息缓存
    std::mutex                      m_mtChatMsgCache;
};

具体代码如下:

添加缓存的思路:

把要缓存的用户id和缓存消息string转化成struct NotifyMsgCache或者struct ChatMsgCache

string后面会加上这个缓存的长度。最后放入到list中。

获取缓存的思路:

根据用户id,在list中遍历查找,如果是用户id,就放入std::list<ChatMsgCache>& cached中。最后就可以在cached中拿到缓存

#include "../base/logging.h"
#include "MsgCacheManager.h"
​
MsgCacheManager::MsgCacheManager()
{
​
}
​
MsgCacheManager::~MsgCacheManager()
{
    
}
​
bool MsgCacheManager::AddNotifyMsgCache(int32_t userid, const std::string& cache)
{
    std::lock_guard<std::mutex> guard(m_mtNotifyMsgCache);
    NotifyMsgCache nc;
    nc.userid = userid;
    nc.notifymsg.append(cache.c_str(), cache.length());;
    m_listNotifyMsgCache.push_back(nc);
    LOG_INFO << "append notify msg to cache, userid: " << userid << ", m_mapNotifyMsgCache.size() : " << m_listNotifyMsgCache.size() << ", cache length : " << cache.length();
    
​
    //TODO: 存盘或写入数据库以防止程序崩溃丢失
​
    return true;
}
​
void MsgCacheManager::GetNotifyMsgCache(int32_t userid, std::list<NotifyMsgCache>& cached)
{
    std::lock_guard<std::mutex> guard(m_mtNotifyMsgCache);
    for (auto iter = m_listNotifyMsgCache.begin(); iter != m_listNotifyMsgCache.end(); )
    {
        if (iter->userid == userid)
        {
            cached.push_back(*iter);
            iter = m_listNotifyMsgCache.erase(iter);
        }
        else
        {
            iter++;
        }
    }
​
   
    LOG_INFO << "get notify msg  cache,  userid: " << userid << ", m_mapNotifyMsgCache.size(): " << m_listNotifyMsgCache.size() << ", cached size: " << cached.size();
}
​
bool MsgCacheManager::AddChatMsgCache(int32_t userid, const std::string& cache)
{
    std::lock_guard<std::mutex> guard(m_mtChatMsgCache);
    ChatMsgCache c;
    c.userid = userid;
    c.chatmsg.append(cache.c_str(), cache.length());
    m_listChatMsgCache.push_back(c);
    LOG_INFO << "append chat msg to cache, userid: " << userid << ", m_listChatMsgCache.size() : " << m_listChatMsgCache.size() << ", cache length : " << cache.length();
    //TODO: 存盘或写入数据库以防止程序崩溃丢失
​
    return true;
}
​
void MsgCacheManager::GetChatMsgCache(int32_t userid, std::list<ChatMsgCache>& cached)
{
    std::lock_guard<std::mutex> guard(m_mtChatMsgCache);
    for (auto iter = m_listChatMsgCache.begin(); iter != m_listChatMsgCache.end(); )
    {
        if (iter->userid == userid)
        {
            cached.push_back(*iter);
            iter = m_listChatMsgCache.erase(iter);
        }
        else
        {
            iter++;
        }
    }
​
    LOG_INFO << "get chat msg cache, no cache,  userid: " << userid << ",m_listChatMsgCache.size(): " << m_listChatMsgCache.size() << ", cached size: " << cached.size();
}

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值