search

Home  >  Q&A  >  body text

如何使用Redis构建复杂数据结构

最近在做一个测试回归的系统,Server这边需要在每个Test执行的时候为它保留相关的数据,待测试结束后,删掉这些数据。

也就是这个数据只是临时的,一般一次测试用例大概就几分种,因此单挑数据的存在时间非常短。所以想要选用Redis做数据存储。

之前没有使用Key-Value数据存储的经验,我这边简单构想的单挑数据结构如下:

{
    sessionId: 123454,
    gloablData: {}, // 在全局维持的数据
    testWins: [
        { winId: 123, parentWinId: 123, stats: 'running', testResult: {}
        }
    ]
}

其中testWinds为测试用所有可能会出现的窗口(窗口其实为浏览器窗口,这个可以无视),然后会为每个窗口记录信息,然后每个窗口有自己的子测试结果testResult,是一个复杂数据(但是可以用JSON字符串来储存,因为Server对它的具体内容不关心,输出给需要的接口就行了),globalData也一样,server不关心,设置成JSON字符串

但是查看Redis文档,发现Redis貌似无法构建这么复杂的数据结构。

因此针对这样的需求,该如何设置这个数据库的结构?

我这边的简单设置是这样的:

对于每次测试:

有一条储存globalData:

session_id:globalData

和多条储存testWin数据:

session_id:testWind_id

其中globalData由于我这边的Server不关心也不会做查询,因此只保存JSON字符串,testResult也一样,这样的话,貌似其实就两种类型:

session_id:testWind_id: { winId: 123, parentWindId: 123, stat: 'running', testResult: JSONString }

session_id:globalData: JSONString
PHPzPHPz2882 days ago987

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-21 10:59:28

    Storage and use can be encapsulated by objects.

    For your situation, I would save it like this.

    Key: "session:123454:globalData" Value: hash The attribute hash is stored here. (hget, hset)
    Key: "sesison:123454:testWins" Value: list stores test wins' ids
    Key: "session:123454:testWins:win_id" Value: hash Here is the hash of the wins attribute, which is your json string.

    Storage like this, you still need to encapsulate it with objects when using it

    reply
    0
  • PHP中文网

    PHP中文网2017-04-21 10:59:28

    Change to MongoDB. It seems that you need a document-based database.

    reply
    0
  • Cancelreply