简体   繁体   English

NODE.JS 缓存 JSON

[英]NODE.JS caching JSON

I have a NODE.JS express server which breaks down chunks of JSON into smaller sections and routes the request via URL's for mobile devices and various other apps I am creating.我有一个 NODE.JS express 服务器,它将 JSON 块分解成更小的部分,并通过 URL 为移动设备和我正在创建的各种其他应用程序路由请求。

I have just moved from test data to live data, but I am finding that NODE.JS is not using the latest version of the JSON, but caching and reusing the JSON that was inplace at the server runtime.我刚刚从测试数据转移到实时数据,但我发现 NODE.JS 没有使用最新版本的 JSON,而是缓存和重用在服务器运行时就位的 JSON。

Here is (part of) the code这是(部分)代码

var express = require('express'),
    http = require('http');
    forever = require('forever');

var ppm = require('./data/ppm.json');
var stations= require('./data/stations.json');
var fgwstations= require('./data/fgwstations.json');

var app = express()
    .use(express.bodyParser())
    .use(express.static('public'));

app.all('/', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    next();
});

app.get('/ppm/all', function (req, res) {
    res.json(ppm);
});
app.get('/*', function (req, res) {
    res.json(404, {status: 'datafeed not found please refer to documentation'});
});

http.createServer(app).listen(3000, function () {
    console.log("Data Server ready at http://localhost:3000");
});

There maybe typos in the code as I have just ripped chunks out as the proper artile is quite long as its does a LOT.代码中可能有错别字,因为我刚刚撕掉了大块,因为正确的文章很长,但它确实很多。 I keep this server running permenantly using the FOREVER function so it will run without the shell command being open我使用 FOREVER 函数使此服务器永久运行,因此它可以在不打开 shell 命令的情况下运行

Now I suspect I could do some kind of fs.filewatch type function that would restart the server every time the JSON file was updated, but this seems a bit iffy restarting a server especially when this data will be updated every 2-3 mins and future ones even more rapidly.现在我怀疑我可以做某种 fs.filewatch 类型的函数,每次更新 JSON 文件时都会重新启动服务器,但这似乎有点不确定重新启动服务器,尤其是当这些数据每 2-3 分钟和未来更新一次时甚至更快。 It only takes one person to be doing a request or an app to be requesting data during this restart to cause a problem.在此重新启动期间,只需一个人执行请求或应用程序请求数据即可导致问题。

Is there a way of 're-reading' the JSON file assigned to var ppm (the others are fairlry static) or is restarting the server the only way?有没有办法“重新读取”​​分配给 var ppm 的 JSON 文件(其他文件都是静态的),或者是重新启动服务器的唯一方法?

Any good ideas on how to read that file and do this would be greatly appreciated as I am sure someone will have a much more efficient way of doing it.关于如何阅读该文件并执行此操作的任何好主意将不胜感激,因为我相信有人会有更有效的方法来做到这一点。

The current dev server is open source (and very much WIP) and feel free to see what it does当前的开发服务器是开源的(并且有很多 WIP),可以随意查看它的作用

http://54.194.148.89:3000

require() caches everything it loads, as it is designed for loading code modules which shouldn't be changing. require()缓存它加载的所有内容,因为它是为加载不应更改的代码模块而设计的。 If you want to reload something, you have to delete the cache entry .如果要重新加载某些内容,则必须删除缓存条目

A better approach would be to instead use a combination of fs.watch , fs.readFile and JSON.parse to reload the changing data.更好的方法是使用fs.watchfs.readFileJSON.parse的组合来重新加载不断变化的数据。 No need to fiddle with the cache or restart the server.无需摆弄缓存或重新启动服务器。

An even better approach would probably be to use a database of some sort instead of the filesystem.更好的方法可能是使用某种数据库而不是文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM