Rtags -- Label Management Module implemented by node. js + redis

Source: Internet
Author: User

Introduction
Labels can be seen everywhere during our webpage tour:

    • Go to the personal Weibo homepage to view tags of yourself/others. The Weibo system will push people with the same tags as you.
    • Visit blog posts. Most blog posts are tagged to describeArticleSubject for convenient search and query
    • Online shopping, we often use tags for product search, such as clicking "winter clothes" + "men" + "coat" to filter clothes

Rtags is a node. js module used for tag management. It uses the Set Data Structure of redis to store tags and related information. (GitHub: https://github.com/bangerlee/rtags.git)

API
Rtags provides the following interfaces:

    1. Add an object and its tag # Add (tags, Id [, FN])
    2. Tag # queryid (ID, FN) of the query object)
    3. Query tags of two objects # queryid (id1, Id2, FN)
    4. Query object tags with specific tags # querytag (tags, FN)
    5. Delete the tag of an object # deltag (tags, Id [, FN])
    6. Delete OBJECT tag # Remove (ID [, FN])

 

Example
First, call Tag # createtag to generate a tag instance and input a string to indicate the category of the object. For example, 'blogs' indicates the blog post and 'clothes' indicates the clothes:
VaR tag = rtags. createtag ('blogs ');

Then, add the objects in this class and the corresponding tag. Tag # Add receives two parameters. The first is the tag of the object. Multiple tags can be separated by commas. The second parameter is the ID of the object, belowCodeUse the STRs subscript as the ID:

 
VaRSTRs =[]; STRs. Push ('Travel, photography, food, music, shopping'); STRs. Push ('Music, party, food, gir'); STRs. Push ('Mac, computer, CPU, memory, disk'); STRs. Push ('Linux, kernel, Linus, 123'); STRs. Push ('Kernel, process, lock, time, Linux'); STRs. foreach (Function(STR, I) {tag. Add (STR, I );});

After the above call, the redis database has the blog Label data, and we can perform related queries. To query the tags of an object, we can call Tag # queryid. This function receives the Object ID and a callback function as the parameter. The query result is stored in IDS as an array:

Tag. queryid (ID= '3'). End (Function(ERR, IDS ){If(ERR)ThrowErr; console. Log ('Tags for "% s ":', ID );VaRTags = IDs. Join (''); Console. Log ('-% S', Tags );});

The above code is used to query the label of a blog post with the ID of '3'. Run the code in this section and the output is:

 
TagsFor"3":-Kernel Linux Linus 1991

To query the tags of two objects, call Tag # queryid. The input parameter must be the ID of the two objects and a callback function:

Tag. queryid (id1= '3', Id2 = '4'). End (Function(ERR, IDS ){If(ERR)ThrowErr; console. Log ('Tags for "% s" and "% s" both have :', Id1, Id2 );VaRTags = IDs. Join (''); Console. Log ('-% S', Tags );});

The preceding code is used to query the tags of a blog with the ID of '3' and '4'. The query result is:

 
TagsFor"3" and "4"Both have:-Kernel Linux

Rtags also provides the ability to search for Objects Based on tags, call Tag # querytag, and input tags and a callback function. If multiple tags exist, they can be separated by commas:

Tag. querytag (tags= 'Music, food'). End (Function(ERR, IDS ){If(ERR)ThrowErr; console. Log ('The objects own the "% s" tags :', Tags );VaRId = IDs. Join (''); Console. Log ('-% S', ID); process. Exit ();});

The preceding Code queries a blog post with both the 'music' and 'food' labels. The output is:

 
The objects own the "Music, food"Tags:-0 1

 

Install
Run the following command to install rtags:

$ NPMInstallRtags

You can also use the following command to obtain the rtags source code from GitHub:

 
$ Git clone git@github.com: bangerlee/rtags. Git

Start redis-server. After installing the shocould module, we can execute the example in the rtags source code directory:

 
$ CD rtags/Test $ node index. js

GitHub address: https://github.com/bangerlee/rtags.git

Welcome to git pull/fork/clone.

Have fun!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.