在服务器上存储和更新JSON数据

在服务器上存储和更新JSON数据,json,store,Json,Store,我的web应用程序应该能够在服务器上存储和更新(同时加载)JSON数据。 但是,数据可能包含一些大数组,每次保存时只追加一个新条目 我的解决方案: 使用json数据中的密钥路径向服务器发送更新 目前,我通过jquery使用xmlhttprequest发送数据,如下所示 /** * Asynchronously writes a file on the server (via PHP-script). * @param {String} file complete filename (path

我的web应用程序应该能够在服务器上存储和更新(同时加载)JSON数据。 但是,数据可能包含一些大数组,每次保存时只追加一个新条目

我的解决方案: 使用json数据中的密钥路径向服务器发送更新

目前,我通过jquery使用xmlhttprequest发送数据,如下所示

/**
 * Asynchronously writes a file on the server (via PHP-script).
 * @param {String} file complete filename (path/to/file.ext)
 * @param content content that should be written. may be a js object.
 * @param {Array} updatePath (optional), json only. not the entire file is written,
 * but the given path within the object is updated. by default the path is supposed to contain an array and the
 * content is appended to it.
 * @param {String} key (optional) in combination with updatePath. if a key is provided, then the content is written
 * to a field named as this parameters content at the data located at the updatePath from the old content.
 *
 * @returns {Promise}
 */
io.write = function (file, content, updatePath, key) {
    if (utils.isObject(content)) content = JSON.stringify(content, null, "\t");
    file = io.parsePath(file);
    var data = {f: file, t: content};
    if (typeof updatePath !== "undefined") {
        if (Array.isArray(updatePath)) updatePath = updatePath.join('.');
        data.a = updatePath;
        if (typeof key !== "undefined") data.k = key;
    }
    return new Promise(function (resolve, reject) {
        $.ajax({
            type: 'POST',
            url: io.url.write,
            data: data,
            success: function (data) {
                data = data.split("\n");
                if (data[0] == "ok") resolve(data[1]);
                else reject(new Error((data[0] == "error" ? "PHP error:\n" : "") + data.slice(1).join("\n")));
            },
            cache: false,
            error: function (j, t, e) {
                reject(e);
                //throw new Error("Error writing file '" + file + "'\n" + JSON.stringify(j) + " " + e);
            }
        });
    });
};
在服务器上,php脚本管理其余部分,如下所示:

  • 接收数据并检查其是否有效
  • 检查给定的文件路径是否可写
  • 如果文件存在且为.json
    • 阅读并解码json
    • 返回关于无效json的错误
  • 如果没有给出更新路径
    • 只要写下数据
  • 如果给定了更新路径
    • 如果无法遍历JSON数据中的更新路径(或文件不存在),则返回错误
  • 在更新路径处更新数据
  • 将打印精美的json写入文件
  • 然而,我并不十分高兴,在过去的几周里,问题不断出现

    我的问题
  • 总的来说:你会如何处理这个问题?其他建议、数据库?有什么图书馆可以帮忙吗?
    注意:我更喜欢只使用php或一些标准apache的解决方案
  • 一个问题是,有时会触发对同一文件的多次写入。为了避免这种情况,我使用了Promissions(包装它,因为我读到jquerys deferred stuff is Not Promission/A Compliance)客户端,但我觉得它不能100%工作。php中是否有跨多个请求工作的(文件)锁
  • JSON文件时不时中断,我不清楚如何重现这个问题。在它破裂的时候,我没有发生过什么的历史。有这样的客户机/服务器保存/加载过程的一般调试策略吗
  • 我编写了一个支持comet的web服务器,它对json数据结构的更新进行区分。出于完全相同的原因。服务器保留几个版本的json文档,并为客户端提供不同版本的json文档,以更新他们需要的json数据的最合理版本
  • 也许你可以重用我的一些代码,用C++和CopeScript编写:

  • 如果您对数据结构具有并发写访问权限,您是否确定在读取文件时,谁会将正确版本的文件写入文件

  • 非常感谢。然而,我不大可能对服务器进行大量修改。但我会看看你的代码!关于3:事实上,我没有有意的并发写访问-如果不应该发生在同一个文件上。