33

Redis只往zset有序集合添加不存在的数据:关键字索引查询构建+源码分析

 5 years ago
source link: http://ourjs.com/detail/5bd5ccf1ac52fe63eba502aa?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Redis的有序集合Sorted Set(zset),可以很方便地用来构建关键字索引表,可以很方便地实现支持超大规模并发的关键字组合条件查询。

比如有套博客系统,博客文章存放在 hash 类型 article:* 中,其中的每个关键字对应的文章存放在 keyword:* 中,则可以用关键字连接查询 ZINTERSTORE 找到文章ID列表:

添加文章

方便起见,以node-redis添加hash为例:

client.hmset('article:001', { title: 'test1', content: '....', keywords: 'redis,技术' })
client.hmset('article:002', { title: 'test2', content: '..', keywords: 'redis' })
client.hmset('article:003', { title: 'test3', content: '....', keywords: 'redis,技术' })
client.hmset('article:004', { title: 'test4', content: '..', keywords: '技术' })

创建索引

zadd keyword:redis 1540736588833 001 1540736588833 002 1540736588833 003
zadd keyword:技术 1540736588833 001 1540736588833 003 1540736588833 004

1540736588833是权重值,是当前时间的毫秒值,代表什么时侯添加的这些关键字。

连接查询

ZINTERSTORE out 2 keyword:技术 keyword:redis

此时out中就会存放包含有技术和redis两个关键字的文章ID,即 001 和 003

只更新不存在的索引

有时侯,我们可能在构建索引时不影响原有索引的权重值,以此来保留每个关键字最初添加时的时间(权重分数)。以此来统计某个时间段添加此关键字的文章。

比如article:004添加了新的关键字redis,而且是和“技术”关键字一起提交的,此时会更新索引:

zadd keyword:技术  1550736588800 004
zadd keyword:redis 1550736588800 004

但是我们不希望 keyword:技术 的权重更新,因为此关键字已经存在了,则直接使用 NX 即可:

zadd keyword:技术   nx  1550736588800 004
zadd keyword:redis  nx  1550736588800 004

然后比如说现在想提取昨天之前添加的“技术“文章ID,则直接按score权重查询即可:

zrangebyscore keyword:技术 0  1550736588800

这在某些场景中非常有用,比如说销售给某些客户添加了“ 无意向客户 “标签,后来在销售的努力下将其转化成了“ 潜在客户 “,为了考核销售员业绩, 需要统计每周/每月的转化个数,可以用此种方法,计算绩效。

还有一些其他的参数:

XX: 仅仅更新存在的成员,不添加新成员。

NX: 不更新存在的成员。只添加新成员。

CH: 修改返回值为发生变化的成员总数,原始是返回新添加成员的总数 (CH 是 changed 的意思)。更改的元素是新添加的成员,已经存在的成员更新分数。 所以在命令中指定的成员有相同的分数将不被计算在内。注:在通常情况下,ZADD返回值只计算新添加成员的数量。

INCR: 当ZADD指定这个选项时,成员的操作就等同ZINCRBY命令,对成员的分数进行递增操作。

同样可以使用XX来更新只存在的成员,可在一些特殊场景中使用。

分析 t_zset.c 的源码可知,这些参数是可以一起使用的,比如incr和XX/NX同时使用:

/* Parse options. At the end 'scoreidx' is set to the argument position
 * of the score of the first score-element pair. */
scoreidx = 2;
while(scoreidx < c->argc) {
    char *opt = c->argv[scoreidx]->ptr;
    if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
    else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX;
    else if (!strcasecmp(opt,"ch")) flags |= ZADD_CH;
    else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR;
    else break;
    scoreidx++;
}

/* Turn options into simple to check vars. */
int incr = (flags & ZADD_INCR) != 0;
int nx = (flags & ZADD_NX) != 0;
int xx = (flags & ZADD_XX) != 0;
int ch = (flags & ZADD_CH) != 0;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK