zoukankan      html  css  js  c++  java
  • rtags——node.js+redis实现的标签管理模块

    引言
    在我们游览网页时,随处可见标签的身影:

    • 进入个人微博主页,可以看到自己/他人的标签,微博系统会推送与你有相同标签的人
    • 游览博文,大多数博文有标签标记,以说明文章主旨,方便搜索和查阅
    • 网上购物,我们经常使用标签进行商品搜索,如点选 “冬装” +  “男士” + “外套” 进行衣物过滤

    rtags就是一个用于标签管理的node.js模块,其使用redis的set数据结构,存放标签和相关信息。(github地址: https://github.com/bangerlee/rtags.git)

    API
    rtags提供以下接口:

    1. 添加物件及其标签  Tag#add(tags, id[, fn])
    2. 查询物件的标签  Tag#queryID(id, fn)
    3. 查询两个物件共有的标签  Tag#queryID(id1, id2, fn)
    4. 查询具有特定标签的物件  Tag#queryTag(tags, fn)
    5. 删除物件的标签  Tag#delTag(tags, id[, fn])
    6. 删除物件  Tag#remove(id[, fn])

     

    示例
    首先调用 Tag#createTag 生成一个 Tag 实例,传入一个字符串指示物件的类别,比如 ‘blogs’ 指示博文,‘clothes’ 指示衣服:
     var tag = rtags.createTag('blogs');

    然后添加该类别的物件和对应的标签,Tag#add 接收两个参数,第一个是物件的标签,有多个标签可用逗号隔开;第二个参数是物件的 id,以下代码中以 strs 下标为 id:

    var strs = [];
    strs.push('travel,photography,food,music,shopping');
    strs.push('music,party,food,girl');
    strs.push('mac,computer,cpu,memory,disk');
    strs.push('linux,kernel,linus,1991');
    strs.push('kernel,process,lock,time,linux');
    
    strs.forEach(function(str, i){ tag.add(str, i); });

    经过上面调用,redis 数据库中就有了博文标签数据,我们就可以进行相关查询了。查询某物件具有哪些标签,我们可以调用 Tag#queryID,该函数接收物件 id 和一个回调函数作为参数,查询结果作为数组存放在 ids 中:

    tag
      .queryID(id = '3')
      .end(function(err, ids){
        if (err) throw err;
        console.log('Tags for "%s":', id);
        var tags = ids.join(' ');
        console.log('  - %s', tags);
      });

    以上代码用于查询 id 为 ‘3’ 的博文的标签,执行该段代码,输出为:

    Tags for "3":
      - kernel linux linus 1991

    要查询两个物件具有哪些相同标签,同样调用 Tag#queryID,这时传入的参数应为两个物件的 id 和一个回调函数:

    tag
      .queryID(id1 = '3', id2 = '4')
      .end(function(err, ids){
        if (err) throw err;
        console.log('Tags for "%s" and "%s" both have:', id1, id2);
        var tags = ids.join(' ');
        console.log('  - %s', tags);
      });

    以上代码用于查询 id 为 ‘3’ 和 ‘4’ 的博文共有的标签,查询结果为:

    Tags for "3" and "4" both have:
      - kernel linux

    rtags 还提供根据标签搜索物件的功能,调用 Tag#queryTag,传入标签和一个回调函数,若有多个标签,可用逗号隔开:

    tag
      .queryTag(tags = 'music,food')
      .end(function(err, ids){
        if (err) throw err;
        console.log('The objects own the "%s" tags:', tags);
        var id = ids.join(' ');
        console.log('  - %s', id);
        process.exit();
      });

    以上代码查询同时具有 ‘music’ 和 ‘food’ 标签的博文,其输出为:

    The objects own the "music,food" tags:
      - 0 1

    安装
    rtags通过以下命令安装,该命令会一同安装rtags依赖的redis模块:

    $ npm install rtags

    亦可以通过以下命令从 github 获取 rtags 源码:

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

    拉起 redis-server,安装 should 模块后,我们可以执行 rtags 源码目录下的例子:

    $ cd rtags/test
    $ node index.js

    github地址: https://github.com/bangerlee/rtags.git

    欢迎 git pull/fork/clone。

    Have fun!

  • 相关阅读:
    Android SingleTask启动模式与Home键的问题
    Flutter Widget截图
    Flutter 以Dialog Activity形式展现
    Flutter Text或者RichText不换行的问题
    Adnroid提高效率之资源移动
    GO学习之 输入输出
    GO学习之 运算符
    GO学习之 值类型与引用类型
    GO学习之 指针
    GO学习之 变量与变量的基本数据类型
  • 原文地址:https://www.cnblogs.com/bangerlee/p/2798680.html
Copyright © 2011-2022 走看看