Something like a tag cache and querying it for suggesting them using Redis

人盡茶涼 提交于 2019-12-01 13:34:06

问题


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:


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

  • http://robots.thoughtbot.com/post/48851498400/redis-partial-word-match-you-auto-complete-me

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!



来源:https://stackoverflow.com/questions/19066462/something-like-a-tag-cache-and-querying-it-for-suggesting-them-using-redis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!