Redis application - tags

Suppose that you need to query both belonging, belonging, and belonging to the situation, taking MySQL as an example, the statement will be very long and consume a lot of resources. And redis can easily solve this problem:


There are several books, each belonging to several tags (types):

    'php bible', 'java bible', 'C++ bible', 'asp bible', 'php from entry to abandon', 'go bible', 'linux kernel programming', 'python bible'

    

belonging to php:

    'php bible', 'php from entry to abandonment'

belonging to the web:

    'php bible', 'asp bible', 'php from entry to abandon'

belonging to code:

    'php bible', 'java bible', 'C++ bible', 'asp bible', 'php from entry to abandon', 'go bible', 'python bible'

belonging to the system:

    'linux kernel programming


Next, the field settings:

    Set up the book first:

        set book:1:title 'php Bible'

        set book:2:title 'java bible'

        set book:3:title 'C++ Bible'

        set book:4:title 'asp Bible'

        set book:5:title 'php from entry to abandon'

        set book:6:title 'go Bible'

        set book:7:title 'linux kernel programming'

        set book:8:title 'python Bible'

    Set bookmarks:

        sadd tag:php    1 5

        sadd tag:web    1 4 5

        sadd tag:code   1 2 3 4 5 6 8

        sadd tag:system 7 


Quickly find out by means of sets and intersections

    Both php tags and web tags:

    127.0.0.1:6379> sinter tag: php tag: web

    1

    5

    It belongs to both php tags, web tags and code tags:

    127.0.0.1:6379> sinter tag:php tag:web tag:code

    1

    5


Get the titles of books 1 and 5:

    127.0.0.1:6379> get book:1:title

    php bible

    127.0.0.1:6379> get book:5:title

    PHP from entry to abandonment


Note: Under normal circumstances, the redis command window does not display Chinese. To display Chinese, add the --raw parameter

    [lzw@localhost bin]$ redis-cli

    127.0.0.1:6379> get book:1:title

    "php\xe5\x9c\xa3\xe7\xbb\x8f"

    127.0.0.1:6379> 

    [lzw@localhost bin]$ redis-cli --raw

    127.0.0.1:6379> get book:1:title

    php bible

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325244186&siteId=291194637