Tag system implementation

来源:互联网 发布:js密码和确认密码校验 编辑:程序博客网 时间:2024/05/17 00:57

Use RDBMS

Give below 2 tables:
Table: BOOK

BOOK_ID BOOK_NAME AUTHOR 1 JAVA JASON 2 RUBY JASON 3 PYTHON JASON 4 PHP JASON 5 OBJECTIVE-C JASON 6 SWIFT JASON

Table: TAG

TAG_ID TAG_NAME BOOK_ID 1 WEB 1 2 WEB 2 3 WEB 3 4 WEB 4 5 APPLE 5 6 APPLE 6 7 PHP 4

If we want to get the books which both have WEB and PHP tag, we need below SQL:

SELECT T1.BOOK_ID FROM TAG T1 INNER JOIN TAG T2         ON T1.BOOK_ID=T2.BOOK_ID AND T1.TAG_NAME='WEB' AND T2.TAG_NAME='PHP'; 

Use Redis

prepare the data:

set book;;bookid;;1;;title JAVAset book;;bookid;;2;;title RUBYset book;;bookid;;3;;title PYTHONset book;;bookid;;4;;title PHPset book;;bookid;;5;;title OBJECTIVE-Cset book;;bookid;;6;;title SWIFTsadd tag;;WEB 1 2 3 4sadd tag;;APPLE 5 6sadd tag;;PHP 4

To get the book both have WEB and PHP tag, use below command:

sinter tag;;WEB tag;;PHP> 4

e.g. for sinter:

key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}SINTER key1 key2 key3 = {c}

To get the book either have WEB or PHP tag, use below command:

sunion tag;;WEB tag;;PHP> 1 2 3 4

e.g. for sunion:

key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}SUNION key1 key2 key3 = {a,b,c,d,e}
0 0
原创粉丝点击