0%

npm介绍

npm(Node Package Manager)是Node.js的包管理器,提供了很多可以重用的模块

使用npm

安装模块

1
2
3
4
// 本地安装 安装在对应的项目下,会生成一个.node_modules的目录
npm install [模块名称]
// 全局安装 可以在所有项目中使用
npm install -g express

下载下来之后就可以在node.js中使用该模块了

1
2
// module为对应模块的名称
var module = require('module')

搜索模块

1
2
// 可以搜索对应的模块
npm search http

查找对应的文档

1
2
// 可以查看对应模块的文档
npm docs axios

CSS语法

CSS语法结构由3部分组成:选择符、属性和值

Selector {Property : Value}

多个标签使用相同的样式

多个标签之间使用逗号隔开

1
2
3
h1,h2,h3,h4 {
color: red
}

嵌套标签样式

当一个标签嵌套在另一个标签中,想要精准的设置该样式,需要先列出上层标签,在列出子标签,中间使用空格隔开

1
2
3
ol ul {
list-style-type: cricle
}

使用class来设置样式

可是使用class来对样式进行分类,以点开头+class名称进行设置

1
2
3
.new {
color: red
}

使用该样式,只需要设置标签的class为new即可

1
<li class="new">测试 </li>

使用id来设置样式

除了可以使用class外,还可以使用id,以井号开头+id值进行设置

1
2
3
#special {
color: red
}

使用该样式,只需要设置标签的id为special即可

1
<li id="special">测试 </li>

标签指定选择符

如果既想使用id或class,又想同时使用标签,可以使用如下格式

1
2
3
4
// 针对id为content的h1标签
h1#content{}
// 针对class为p1的h1标签
h1.p1{}

组合选择符

1
2
3
4
5
6
7
8
// h1标签下的所有class为p1的标签
h1 .p1{}

// id为content的标签下所有h1标签
#content h1{}

//id为content的h1标签下的h2标签
h1#content h2{}

列表样式

编号列表的标记符为<ol>,表示顺序列表

1
2
3
4
<ol>
<li>aaaa</li>
<li>bbbb</li>
</ol>

效果为

顺序列表

项目符号列表的标记符为<ul>,表示无序列表

1
2
3
4
<ul>
<li>aaaa</li>
<li>bbbb</li>
</ul>

效果为

无序列表

<li>则使用在<ol><ul>中,来封装列表中的每一项

除此之外,还可以通过list-style-type属性来设置样式,其可选项为

阅读全文 »

quicklist实现

List属于单值多value,链表用的是双向链表结构,list支持pop、push来操作头部和尾部,既可以用做栈也可以用做队列

在3.2版本之前使用的是ziplist和linkedlist,在3.2版本之后使用的是quicklist

quicklist结构

由于linkedlist的附加空间相对太高,prev和next指针就要占去16个字节,而且每个节点的内存都是单独分配,加剧了内存的碎片化,使用quicklist来代替之前的ziplist+linkedlist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
typedef struct quicklist {
quicklistNode *head;
quicklistNode *tail;
// 元素总数量
unsigned long count; /* total count of all entries in all ziplists */
// 元素总字节
unsigned long len; /* number of quicklistNodes */
int fill : QL_FILL_BITS; /* fill factor for individual nodes */
unsigned int compress : QL_COMP_BITS; /* depth of end nodes not to compress;0=off */
unsigned int bookmark_count: QL_BM_BITS;
quicklistBookmark bookmarks[];
} quicklist;

typedef struct quicklistNode {
struct quicklistNode *prev;
struct quicklistNode *next;
unsigned char *zl; // 对应的是ziplist
// ziplist的字节总数
unsigned int sz; /* ziplist size in bytes */
// ziplist中的元素数量
unsigned int count : 16; /* count of items in ziplist */
unsigned int encoding : 2; /* RAW==1 or LZF==2 */
unsigned int container : 2; /* NONE==1 or ZIPLIST==2 */
unsigned int recompress : 1; /* was this node previous compressed? */
unsigned int attempted_compress : 1; /* node can't compress; too small */
unsigned int extra : 10; /* more bits to steal for future usage */
} quicklistNode;

看代码好像底层还是使用的ziplist,而且还有前驱和后继指针,就是一个ziplist+linkedlist混合体,把多个ziplist使用双向指针串联起来了。

默认单个ziplist长度为8字节,使用 list-max-ziplist-size来进行配置

1
2
3
4
5
6
7
8
9
10
# For a fixed maximum size, use -5 through -1, meaning:
# -5: max size: 64 Kb <-- not recommended for normal workloads
# -4: max size: 32 Kb <-- not recommended
# -3: max size: 16 Kb <-- probably not recommended
# -2: max size: 8 Kb <-- good
# -1: max size: 4 Kb <-- good
# Positive numbers mean store up to _exactly_ that number of elements
# per list node.
# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),
# but if your use case is unique, adjust the settings as necessary.

IO模型

linux中有几种IO模型,如select、poll、epoll,这几个分别是什么呢?

select模型

在select模型下是利用轮询socket句柄的方式来实现监测socket中是否有IO数据到达,每次调用select,都需要把fd集合从用户态拷贝到内核态,然后在内核态还要遍历一遍传进来的所有fd,在fd很多时开销很大

select默认支持的文件描述符是1024

poll模型

poll模型与select类似,不过其是基于链表存储的,没有最大连接数限制,但是跟select一样,需要把fd集合在用户态和内核态之间来回复制

epoll模型

epoll改进了轮询socket句柄的方式,采用notify机制来进行监测,为每个fd指定一个回调函数,当设备就绪,唤醒等待队列上的等待者,就会调用这个回调函数