Something like a tag cache and querying it for sug

2019-02-21 06:10发布

It would be like StackOverflow: when you ask a question you need to provide some tags.

Currently I'm querying the relational database store, but I believe that Redis should make sense in order to cache tag suggestions.

For example, it would be a set like this:

sadd tags:suggestions "c#" ".net" "redis"

Now some user is asking a question and he/she may write "ne" so there's some tag in the Redis cache that may match the whole partial tag name: .net.

I can't figure out how I would intersect such tags:suggestions Redis set in order to get ".net".

Or should I use a string instead of a set?

Thank you in advance!

Note:

For those asking "what I've tried so far", please double-check the question: I can't figure out what to do, I'm just learning Redis. What I've tried so far? Reading the manual, trying it using a set, but I came here because I don't know if I can implement such requirement with Redis...

1条回答
相关推荐>>
2楼-- · 2019-02-21 06:21

After googling a lot, I found a good post about something that fits what I was asking for here at StackOverflow:

Summary...:

1. Create key-values for tags

sadd mysite:tags "stackoverflow" "stack-exchange" "question" "about-redis"

2. Create an index for each possible combination

Yes, for example:

  • "s"
  • "st"
  • "sta"
  • ... and so on

    sadd mysite:tags:index:s 1 2

    sadd mysite:tags:index:st 1 2

    sadd mysite:tags:index:sta 1 2

    sadd mysite:tags:index:stack 1 2

    sadd mysite:tags:index:stacko 1

... and so on.

It's about adding all tags that start with s, st...

3. Using SORT to get tags suggestions:

sort mysite:tags:index:s by nosort get tags:*

This will output:

  • stackoverflow
  • stack-exchange

Or... sort mysite:tags:index:stack- by nosort get tags:*

...will output:

  • stack-exchange

It seems to be a good solution!

查看更多
登录 后发表回答