登录
首页 >  数据库 >  MySQL

DCache 分布式存储系统|Set, ZSet 缓存模块的创建与使用

来源:SegmentFault

时间:2023-02-17 15:11:37 139浏览 收藏

你在学习数据库相关的知识吗?本文《DCache 分布式存储系统|Set, ZSet 缓存模块的创建与使用》,主要介绍的内容就涉及到MySQL、缓存、NoSQL、nosql-分布式、tars,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

作者 | Eaton

导语 | 在之前的系列文章中,我们介绍了 DCache 及其 KV, K-K-Row 和 List 缓存模块的使用,本文将继续介绍如何使用 DCache 中的集合类型缓存模块 —— Set 和 ZSet 缓存模块。

系列文章

目录

  • Set 与 ZSet 模块简介
  • 创建 Set/ZSet 缓存模块
  • 调用 Set/ZSet 缓存模块服务

    • Set 模块读写操作
    • ZSet 模块读写操作
    • 实例
  • 其它 Set/ZSet 缓存模块服务接口
  • 总结

DCache 是一个基于 TARS 框架开发的分布式 NoSQL 存储系统,支持多种数据结构,包括了

1
2
3
4
5
6
7
8
// main.cpp
 
...
 
static string ModuleTestDemoSet   = "TestDemoSet";
static string ModuleTestDemoZSet  = "TestDemoZSet";
 
...

接口调用流程与 TARS 服务接口调用流程一致。如果你还不清楚 TARS 服务的调用方式和流程,可以阅读文章 TARS RPC 通信框架|提供多种远程调用方式 了解 TARS 服务的调用方式。

后面的示例中,会使用到三个工具函数,定义如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 构建 UpdateValue
DCache::UpdateValue genUpdateValue(DCache::Op op, const string &value)
{
    DCache::UpdateValue updateValue;
    updateValue.op = op;
    updateValue.value = value;
    return updateValue;
}
 
// 打印 map<string string=""> 类型数据
void printMapData(const map<string string=""> &data)
{
    map<string string="">::const_iterator it = data.begin();
    while (it != data.end())
    {
        cout first second;
        ++it;
    }
    cout  数据
void printVectorMapData(const vector<map string="">> &data)
{
    for (auto item : data)
    {
        printMapData(item);
    }
}</map></string></string></string>

那么接下来,我们来看看怎么使用 DCache 的 Set/ZSet 缓存模块。

Set 模块读写操作

Set 为集合缓存模块。这里介绍写接口

1
int addSet(const AddSetReq &req)

其中结构

1
2
3
4
5
6
7
8
9
10
11
12
13
struct AddSetReq
{
  1 require string moduleName;     // 模块名
  2 require AddSetKeyValue value;  // 待写入数据
};
 
struct AddSetKeyValue
{
  1 require string mainKey;  // 主key
  2 require map<string updatevalue=""> data;  // 其他字段数据
  3 require int expireTime;  // 过期时间
  4 require bool dirty = true// 是否设置为脏数据
};</string>

使用示例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void testAddSet(const string &mainKey, const map<string string=""> &data, DCache::ProxyPrx prx)
{
    cout ::const_iterator it = data.begin();
    while (it != data.end())
    {
        req.value.data[it->first] = genUpdateValue(DCache::SET, it->second);
        ++it;
    }
 
    int ret = prx->addSet(req);
 
    if (ret == DCache::ET_SUCC)
    {
        printMapData(data);
        return;
    }
    cout </string>

获取集合

接口

1
int getSet(const GetSetReq &req, BatchEntry &rsp)

其中请求消息结构

1
2
3
4
5
6
7
8
9
10
11
12
struct GetSetReq
{
  1 require string moduleName;  //模块名
  2 require string mainKey;  //主key
  3 require string field;  //需要查询的字段集,多个字段用','分隔如 "a,b", "*"表示所有
  4 require string idcSpecified = ""//idc区域
};
 
struct BatchEntry
{
  1 require vector<map string="">> entries;  //查询结果集合
};</map>

使用示例如下

1
2
3
4
5
6
7
8
9
10
11
void testGetSet(const string &mainKey, DCache::ProxyPrx prx)
{
    cout getSet(req, rsp);
 
    if (ret == DCache::ET_SUCC)
    {
        // 打印返回值
        printVectorMapData(rsp.entries);
        return;
    }
    cout

ZSet 模块读写操作

ZSet 即有序集合缓存模块,这里介绍写接口

1
int addZSet(const AddZSetReq &req)

其中请求消息结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AddZSetReq
{
  1 require string moduleName;  //模块名
  2 require AddSetKeyValue value;  //待写入数据
  3 require double score;  //待写入数据分值
};
 
struct AddSetKeyValue
{
  1 require string mainKey;  //主key
  2 require map<string updatevalue=""> data; //其他字段数据
  3 require int expireTime;  //数据过期时间
  4 require bool dirty = true//是否设置为脏数据
};</string>

使用示例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void testAddZSet(const string &mainKey, const map<string string=""> &data, const double &score, DCache::ProxyPrx prx)
{
    cout ::const_iterator it = data.begin();
    while (it != data.end())
    {
        req.value.data[it->first] = genUpdateValue(DCache::SET, it->second);
        ++it;
    }
 
    int ret = prx->addZSet(req);
 
    if (ret == DCache::ET_SUCC)
    {
        printMapData(data);
        return;
    }
    cout </string>

获取集合

接口

1
int getZSetByPos(const GetZsetByPosReq &req, BatchEntry &rsp)

其中请求消息结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct GetZsetByPosReq
{
  1 require string moduleName;  //模块名
  2 require string mainKey;  //主key
  3 require string field;  //需要查询的字段集,多个字段用','分隔如 "a,b", "*"表示所有
  4 require long start;  //开始索引
  5 require long end;  //结束索引
  6 require bool positiveOrder = true; //true表示返回的结果按递增排序,false表示递减
  7 require string idcSpecified = ""//idc区域
};
 
struct BatchEntry
{
  1 require vector<map string="">> entries;  //查询结果数据集合
};</map>

使用示例如下

1
2
3
4
5
6
7
8
9
10
11
void testGetZSet(const string &mainKey, DCache::ProxyPrx prx)
{
    cout getZSetByPos(req, rsp);
 
    if (ret == DCache::ET_SUCC)
    {
        // 打印返回值
        printVectorMapData(rsp.entries);
        return;
    }
    cout

实例

我们来实际运行一下上面的使用示例。完整的使用示例可以在 GitHub 仓库 DCacheDemo 中获取。

我们通过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void testSet(DCache::ProxyPrx prx)
{
    cout  data1, data2, data3, data4;
    data1["VALUE"] = "hello";
    data2["VALUE"] = "hello";
    data3["VALUE"] = "hi";
    data4["VALUE"] = "test";
 
    testAddSet(mainKey, data1, prx);
    testAddSet(mainKey, data2, prx);
    testAddSet(mainKey, data3, prx);
    testAddSet(mainKey, data4, prx);
 
    testGetSet(mainKey, prx);
 
    cout  data1, data2, data3, data4;
    double score1, score2, score3, score4;
    data1["VALUE"] = "hello";
    score1 = 0.1;
    data2["VALUE"] = "hello";
    score2 = 0.1;
    data3["VALUE"] = "hi";
    score3 = 0.8;
    data4["VALUE"] = "test";
    score4 = 0.5;
 
    testAddZSet(mainKey, data1, score1, prx);
    testAddZSet(mainKey, data2, score2, prx);
    testAddZSet(mainKey, data3, score3, prx);
    testAddZSet(mainKey, data4, score4, prx);
 
    testGetZSet(mainKey, prx);
 
    cout

接着,在

1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char *argv[])
{
    ...
 
        auto prx = comm->stringToProxy<:proxyprx>(DCacheTestDemoObj);
 
        // 调用 DCache 缓存服务
        testSet(prx);
        testZSet(prx);
    ...
}<!--:proxyprx-->

执行结果如下

可以看到,尽管我们插入了两次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**************** Set ****************/
// 查询集合数据
int getSet(GetSetReq req, out BatchEntry rsp);
// 向集合添加数据
int addSet(AddSetReq req);
// 删除指定的一条集合数据
int delSet(DelSetReq req);
 
/**************** ZSet ****************/
// 根据指定条件,查询某条记录的 score 值
int getZSetScore(GetZsetScoreReq req, out double score);
// 根据指定条件,查询某条记录在已排序列表的索引位置
int getZSetPos(GetZsetPosReq req, out long pos);
// 查询集合内指定索引区间[start, end]内的数据
int getZSetByPos(GetZsetByPosReq req, out BatchEntry rsp);
// 查询分值区间[minScore, maxScore]内的数据
int getZSetByScore(GetZsetByScoreReq req, out BatchEntry rsp);
// 将带有给定分值的数据添加到有序集合中,如果数据已存在,则重置 score 值
int addZSet(AddZSetReq req);
// 修改有序集合中某条记录的分值,若数据不存在,则新建一条数据
int incScoreZSet(IncZSetScoreReq req);
// 删除有序集合中符合指定条件的某条数据
int delZSet(DelZSetReq req);
// 从有序集合中删除分值在区间[minScore, maxScore)的数据
int delZSetByScore(DelZSetByScoreReq req);
// 根据指定条件更新有序集合的某条数据
int updateZSet(UpdateZSetReq req);

接口的使用方式与前面介绍的类似,关于接口的具体入参和出参结构可以参考 Proxy 接口指南

总结

本文简要介绍了 DCache 中的

1
set
1
zset
缓存模块的原理和使用流程,同时通过具体实例对部分接口的使用进行了详细介绍,帮助读者理解并能够快速上手使用
1
set
1
zset
缓存模块。

TARS 可以在考虑到易用性和高性能的同时快速构建系统并自动生成代码,帮助开发人员和企业以微服务的方式快速构建自己稳定可靠的分布式应用,从而令开发人员只关注业务逻辑,提高运营效率。多语言、敏捷研发、高可用和高效运营的特性使 TARS 成为企业级产品。

TARS微服务助您数字化转型,欢迎访问:

TARS官网:https://TarsCloud.org

TARS源码:https://github.com/TarsCloud

Linux基金会官方微服务免费课程:https://www.edx.org/course/bu...

获取《TARS官方培训电子书》:https://wj.qq.com/s2/7849909/...

或扫码获取:

QR

今天关于《DCache 分布式存储系统|Set, ZSet 缓存模块的创建与使用》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表