Nodejs 内存存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2219333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:36:31  来源:igfitidea点击:

Nodejs in-memory storage

storagenode.js

提问by Ionu? G. Stan

How do I store data to be used for all the clients in my server? (like the messages of a chat)

如何存储要用于服务器中所有客户端的数据?(就像聊天的消息)

回答by Ionu? G. Stan

The server that node.js allows you to build, is an application server, which means that state is preserved, between request, on the server side. The following snippet demonstrates this:

node.js 允许您构建的服务器是一个应用程序服务器,这意味着状态在请求之间保留在服务器端。以下代码段演示了这一点:

var sys  = require('sys'),
    http = require('http');

var number = 0;

http.createServer(function (req, res) {
        console.log(req.method, req.url);

        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<h1>Number is: ' + number + '</h1>');
        res.end();

        number++;

}).listen(8000);

sys.puts('Server running at http://127.0.0.1:8000/');

回答by Bharani

If you want more features have a look at redis-node-client

如果您想要更多功能,请查看redis-node-client

回答by iajnr

node-cachepackage is currently the best for key value store and it allows synchronous as well as async storage/retrieval/deletion of keys.

node-cache包是目前最好的键值存储,它允许同步和异步存储/检索/删除键。

npm link

npm 链接

回答by christkv

Or use a native node storage mechanism (written in node.js)

或者使用原生节点存储机制(用node.js编写)

http://github.com/felixge/node-dirty

http://github.com/felixge/node-dirty

回答by tylucaskelley

I wrote Bxfor this purpose; it gives you a simple in-memory cache with:

我为此编写了Bx;它为您提供了一个简单的内存缓存:

  • Key-value storage
  • Optional expiration on any stored data
  • Support for schemas using JSON-schema
  • 键值存储
  • 任何存储数据的可选过期
  • 支持使用 JSON 模式的模式

Although I'm plugging my own repository here, I can assure you it works well and it's been used in production at my own company, Onshapefor over a year without issues. At the end of the day it's a pretty simple tool; not much to mess up here.

尽管我在这里插入了我自己的存储库,但我可以向您保证它运行良好,并且它已在我自己的公司Onshape 的生产中使用了一年多,没有出现任何问题。归根结底,它是一个非常简单的工具;这里没有什么可搞砸的。

However, if you're storing data that's meant to be permanent you're going to want a database such as MongoDB (w/ Mongoose), MySQL, etc. rather than a cache like Bx or Redis.

但是,如果您要存储永久的数据,您将需要一个数据库,例如MongoDB (w/ Mongoose)MySQL等,而不是像 Bx 或Redis这样的缓存。