C++ 一致性缓存的测试

关于一致性hash网上的例子很多, 原理在网上也能找得到,随便实现了一个,没有虚拟节点的。为了图方便也只是用的list和sort,如果真的要写的很好的话就要自己写一个环状的数据结构

#include <iostream>
#include <functional> //hash
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cstdio>
typedef unsigned long long ull;
using namespace std;
bool cmp(const pair<string, unsigned long long> &a, const pair<string, unsigned long long> &b)
{
    return a.second < b.second;
}
class hash_consistent
{
    std::hash<string> getHash;
    ull h(const string &host)
    {
        return (ull)(getHash(host)) % (((ull)INT32_MAX) + 1);
    }

public:
    map<string, map<string, string>> hostList;       //使用map有序排列各个host
    vector<pair<string, unsigned long long>> circle; //用vector保存哈希环,用于决定缓存的存放位置
    void addServer(const string &host)
    {
        if (hostList.count(host) != 0)
            return; //主机已经上线,则返回
        circle.push_back(
            make_pair(
                host,
                h(host)));
        sort(circle.begin(), circle.end(), cmp); //按照hash排序
        hostList[host].emplace();
    }
    bool set(const string &key, const string &val)
    {
        //在这里统一计算hash并且插入
        ull hash = h(key);
        for (int i = 1; i < circle.size(); i++)
        {
            if (hash > circle[i].second)
            {
                continue;
            }
            else
            {
                printf("%s:%s \t\t的hash为:%ull,被保存在hash为:%ull的服务器上\n", key.c_str(),val.c_str(), hash, circle[i - 1].second);
                hostList[circle[i - 1].first][key] = val;
                return true;
            }
        } //从1找,都没有找到,则说明将会插在第0个节点,因为是个环
        printf("%s:%s \t\t的hash为:%ull,被保存在hash为:%ull的服务器上\n", key.c_str(),val.c_str(), hash, circle[0].second);
        hostList[circle[0].first][key] = val;
        return true;
    }
    string get(const string& key){
        ull hash=h(key);//计算hash
        for (int i = 1; i < circle.size(); i++)
        {
            if (hash > circle[i].second)
            {
                continue;
            }
            else
            {
                if(hostList[circle[i-1].first].count(key)==0)return "找不到";
                return hostList[circle[i - 1].first][key];
            }
        }
        if(hostList[circle[0].first].count(key)==0)return "找不到";
        return hostList[circle[0].first][key];
    }
};
int main()
{
    hash_consistent hc;
    hc.addServer("127.12.22.5:4555");
    hc.addServer("127.12.2.2:4555");
    hc.addServer("127.4.12.3:4555");
    hc.addServer("192.0.1.4:4555");
    
    hc.set("key1", "123456");
    hc.set("key2", "6666");
    hc.set("key3", "zxcvbnm");
    hc.set("key4", "哈哈哈哈");
    hc.set("key5", "123123456");
    hc.set("百度", "www.baidu.com");
    hc.set("谷歌", "www.google.com");
    hc.set("雅虎", "www.yahoo.com");

    cout<<"-----------------------"<<endl;
    string key;
    while(true){
        cin>>key;
        if(key=="q"){
            break;
        }
        cout<<hc.get(key)<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值