Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application of redis-- label

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Mysql:

Create table book (

Bookid int

Title char (20)

) engine myisam charset utf8

Insert into book values

(5, 'PHP Bible')

(6, 'ruby actual combat')

(7, 'mysql operation and maintenance')

(8, 'ruby server programming')

Create table tags (

Tid int

Bookid int

Content char (20)

) engine myisam charset utf8

Insert into tags values

(10,5, 'PHP')

(11,5, 'WEB')

(12, 6, 'WEB')

(13, 6, 'ruby')

(14, 7, 'database')

(15,8, 'ruby')

(16, 8, 'server')

# Books with both web tags and PHP tags and tags should be queried with links

Select * from tags inner join tags as t on tags.bookid=t.bookid

Where tags.content='PHP' and t. Contentment

Replace it with key-value storage

Use kv to store

Set book:5:title 'PHP Bible'

Set book:6:title 'ruby actual Battle'

Set book:7:title 'mysql Shipping disaster'

Set book:8:title 'ruby server'

Sadd tag:PHP 5

Sadd tag:WEB 5 6

Sadd tag:database 7

Sadd tag:ruby 6 8

Sadd tag:SERVER 8

Check: there are books by both PHP and WEB

Sinter tag:PHP tag:WEB # check the intersection of sets

Check: books with PHP or WEB tags

Sunin tag:PHP tag:WEB

Check: books that contain ruby and no WEB tags

Sdiff tag:ruby tag:WEB # difference set

Suppose you need to query cases that both belong to, belong to, and belong to. Take mysql as an example, using join (if there are enough overlapping tags, the amount of join will be enough), the statement will be very long and resource-consuming. Redis can easily solve this problem:

There are several books, which belong to several labels (types):

'php Bible', 'java Bible', 'C++ Bible', 'asp Bible', 'php from introduction to giving up','go Bible', 'linux kernel programming', 'python Bible'

Belonging to php:

"php Bible", "php from getting started to giving up"

Belonging to web:

"php Bible", "asp Bible", "php from introduction to giving up"

Belonging to code:

'php Bible', 'java Bible', 'C++ Bible', 'asp Bible', 'php from introduction to abandonment','go Bible', 'python Bible'

Belonging to 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 's C++ Bible

Set book:4:title 'asp Bible'

Set book:5:title 'php from getting started to giving up'

Set book:6:title'go Bible'

Set book:7:title 'linux kernel programming'

Set book:8:title 'python Bible'

Set the bookmark:

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 the way of set and intersection

Belonging to both php and web tags:

127.0.0.1 purl 6379 > sinter tag:php tag:web

one

five

Belonging to both the php tag and the web tag and the code tag:

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

one

five

Get the title of the book 1 and 5:

127.0.0.1 purl 6379 > get book:1:title

Php Bible

127.0.0.1 purl 6379 > get book:5:title

Php from getting started to giving up

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

[lzw@localhost bin] $redis-cli

127.0.0.1 purl 6379 > get book:1:title

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

127.0.0.1purl 6379 >

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

127.0.0.1 purl 6379 > get book:1:title

Php Bible

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report