Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
126
第 13章
持久化
除了最简单的网站和
Web
应用,其他所有网站和
Web
应用都需要某种形式的
持久化
。也
就是说,只有拥有比易失的内存存储更持久的方式,你的数据才能在经历服务器崩溃、停
电、系统升级和数据迁移之后仍然完好无损。本章将首先讨论持久化的各种选择,然后分
别对文档数据库和关系型数据库做一些演示。不过在进入数据库之前,我们先从最基本的
持久化方式,即文件系统持久化开始。
13.1
 文件系统持久化
实现持久化的一种方式是直接把数据保存到所谓的扁平文件里(之所以用
扁平
来形容,是
因为文件内并没有结构,它只是字节的序列)。在
Node
中实现文件系统持久化要使用
fs
(文件系统)模块。
文件系统持久化有一些不足,尤其是它的伸缩性不太好。随着网站流量增长,当你需要更
多的服务器时,除非所有的服务器都能访问到一个共享的文件系统,否则持久化就会暴露
问题。此外,因为扁平文件本身没有结构,所以数据的定位、排序和筛选都要在你的应用
上进行,这对应用造成了负担。由于这些原因,你应该首先考虑用数据库而不是文件系统
来存储数据。但有一个例外是存储二进制文件,比如图片、音频或视频。尽管很多数据库
能处理二进制数据,但很少能比文件系统更高效(不过为了能够搜索、排序和筛选,这些
二进制文件的相关信息通常是存储在数据库里的)。
如果确实需要存储二进制数据,那么要记得文件系统存储始终存在伸缩性不好的问题。如
果应用的托管服务不能访问到一个共享的文件系统(经常会这样),就应该考虑把二进制
持久化
|
127
文件存储到数据库(通常需要做一点儿配置 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099