I'm looking for fast and efficient data storage to build my PHP based web site. I'm aware of MySql. Can I use a JSON file in my server root directory instead of a MySQL database? If yes, what is the best way to do it?
我正在寻找快速有效的数据存储,以构建基于PHP的web站点。我意识到MySql。我可以在服务器根目录中使用JSON文件而不是MySQL数据库吗?如果是,最好的方法是什么?
3 个解决方案
#1
19
You can use any single file, including a JSON file, like this:
您可以使用任何一个文件,包括JSON文件,如下所示:
-
Lock it somehow (google PHP file locking, it's possibly as simple as adding a parameter to file open function or changing function name to locking version).
以某种方式锁定它(谷歌PHP文件锁定,它可能简单到在文件打开函数中添加一个参数或将函数名更改为锁定版本)。
-
Read the data from file and parse it to internal data stucture.
从文件中读取数据并将其解析为内部数据结构。
-
Optionally modify the data in internal data structure.
可选地修改内部数据结构中的数据。
-
If you modified the data, truncate the file to 0 length and write new data to it.
如果修改了数据,将文件截断为0,并向其写入新数据。
-
Unlock the file as soon as you can, other requests may be waiting...
尽快解锁文件,其他请求可能正在等待……
-
You can keep using the data in internal structures to render the page, just remember it may be out-dated as soon as you release the file lock and other HTTP request can modify it.
您可以继续使用内部结构中的数据来呈现页面,只要记住,一旦释放文件锁,其他HTTP请求就可以修改页面。
Also, if you modify the data from user's web form, remember that it may have been modified in between. Like, load page with user details for editing, then other user deletes that user, then editer tries to save the changed details, and should probably get error instead of re-creating deleted user.
此外,如果您修改用户web表单中的数据,请记住,它可能在其中进行了修改。例如,加载带有用户详细信息的页面进行编辑,然后其他用户删除该用户,然后编辑器尝试保存已更改的详细信息,可能会得到错误,而不是重新创建已删除的用户。
Note: This is very inefficient. If you are building a site where you expect more than say 10 simultaneous users, you have to use a more sophisticated scheme, or just use existing database... Also, you can't have too much data, because parsing JSON and generating modified JSON takes time.
注意:这是非常低效的。如果您正在构建一个站点,希望同时有超过10个用户,那么您必须使用更复杂的方案,或者仅仅使用现有的数据库……此外,您不能有太多的数据,因为解析JSON和生成修改后的JSON需要时间。
As long as you have just one user at a time, it'll just get slower and slower as amount of data grows, but as user count increases, and more users means both more requests and more data, things start to get exponentially slower and you very soon hit limit where HTTP requests start to expire before file is available for handling the request...
只要你一次只有一个用户,它会变得越来越慢随着数据量的增长,但是随着用户数量的增加,和更多的用户意味着更多的请求和数据,事情开始变得成倍增长慢,你很快达到极限,HTTP请求开始到期之前文件用于处理请求……
At that point, do not try to hack it to make it faster, but instead pick some existing database framework (SQL or nosql or file-based). If you start hacking together your own, you just end up re-inventing the wheel, usually poorly :-). Well, unless it is just programming exercise, but even then it might be better to instead learn use of some existing framework.
此时,不要试图破解它以使其更快,而是选择一些现有的数据库框架(SQL或nosql或基于文件的)。如果你开始自己动手,你最终只是重新发明了*,通常很糟糕:-)。好吧,除非这只是编程练习,但即便如此,学习使用一些现有框架可能更好。
#2
1
The new version of IBM Informix 12.10 xC2 supports now JSON.
check the link : http://pic.dhe.ibm.com/infocenter/informix/v121/topic/com.ibm.json.doc/ids_json_007.htm
IBM Informix 12.10 xC2的新版本现在支持JSON。检查链接:http://pic.dhe.ibm.com/infocenter/informix/121/topic/com.ibm.json.doc/ids_json_007.htm
The manual says it is compatible with MongoDB drivers.
手册上说它与MongoDB驱动程序兼容。
About the Informix JSON compatibility
关于Informix JSON兼容性
Applications that use the JSON-oriented query language, created by MongoDB, can interact with data stored in Informix® databases. The Informix database server also provides built-in JSON and BSON (binary JSON) data types.
应用程序使用JSON-oriented查询语言,由MongoDB,可以与Informix®数据库中存储的数据交互。Informix数据库服务器还提供内置的JSON和BSON(二进制JSON)数据类型。
You can use MongoDB community drivers to insert, update, and query JSON documents in Informix.
可以使用MongoDB社区驱动程序在Informix中插入、更新和查询JSON文档。
Not sure, but I believe you can use the Innovator-C edition (free for production) to test and use it with no-cost either for production enviroment.
不确定,但是我相信您可以使用Innovator-C edition(免费用于生产)来测试和使用它,而且对于生产环境都是免费的。
#3
#1
19
You can use any single file, including a JSON file, like this:
您可以使用任何一个文件,包括JSON文件,如下所示:
-
Lock it somehow (google PHP file locking, it's possibly as simple as adding a parameter to file open function or changing function name to locking version).
以某种方式锁定它(谷歌PHP文件锁定,它可能简单到在文件打开函数中添加一个参数或将函数名更改为锁定版本)。
-
Read the data from file and parse it to internal data stucture.
从文件中读取数据并将其解析为内部数据结构。
-
Optionally modify the data in internal data structure.
可选地修改内部数据结构中的数据。
-
If you modified the data, truncate the file to 0 length and write new data to it.
如果修改了数据,将文件截断为0,并向其写入新数据。
-
Unlock the file as soon as you can, other requests may be waiting...
尽快解锁文件,其他请求可能正在等待……
-
You can keep using the data in internal structures to render the page, just remember it may be out-dated as soon as you release the file lock and other HTTP request can modify it.
您可以继续使用内部结构中的数据来呈现页面,只要记住,一旦释放文件锁,其他HTTP请求就可以修改页面。
Also, if you modify the data from user's web form, remember that it may have been modified in between. Like, load page with user details for editing, then other user deletes that user, then editer tries to save the changed details, and should probably get error instead of re-creating deleted user.
此外,如果您修改用户web表单中的数据,请记住,它可能在其中进行了修改。例如,加载带有用户详细信息的页面进行编辑,然后其他用户删除该用户,然后编辑器尝试保存已更改的详细信息,可能会得到错误,而不是重新创建已删除的用户。
Note: This is very inefficient. If you are building a site where you expect more than say 10 simultaneous users, you have to use a more sophisticated scheme, or just use existing database... Also, you can't have too much data, because parsing JSON and generating modified JSON takes time.
注意:这是非常低效的。如果您正在构建一个站点,希望同时有超过10个用户,那么您必须使用更复杂的方案,或者仅仅使用现有的数据库……此外,您不能有太多的数据,因为解析JSON和生成修改后的JSON需要时间。
As long as you have just one user at a time, it'll just get slower and slower as amount of data grows, but as user count increases, and more users means both more requests and more data, things start to get exponentially slower and you very soon hit limit where HTTP requests start to expire before file is available for handling the request...
只要你一次只有一个用户,它会变得越来越慢随着数据量的增长,但是随着用户数量的增加,和更多的用户意味着更多的请求和数据,事情开始变得成倍增长慢,你很快达到极限,HTTP请求开始到期之前文件用于处理请求……
At that point, do not try to hack it to make it faster, but instead pick some existing database framework (SQL or nosql or file-based). If you start hacking together your own, you just end up re-inventing the wheel, usually poorly :-). Well, unless it is just programming exercise, but even then it might be better to instead learn use of some existing framework.
此时,不要试图破解它以使其更快,而是选择一些现有的数据库框架(SQL或nosql或基于文件的)。如果你开始自己动手,你最终只是重新发明了*,通常很糟糕:-)。好吧,除非这只是编程练习,但即便如此,学习使用一些现有框架可能更好。
#2
1
The new version of IBM Informix 12.10 xC2 supports now JSON.
check the link : http://pic.dhe.ibm.com/infocenter/informix/v121/topic/com.ibm.json.doc/ids_json_007.htm
IBM Informix 12.10 xC2的新版本现在支持JSON。检查链接:http://pic.dhe.ibm.com/infocenter/informix/121/topic/com.ibm.json.doc/ids_json_007.htm
The manual says it is compatible with MongoDB drivers.
手册上说它与MongoDB驱动程序兼容。
About the Informix JSON compatibility
关于Informix JSON兼容性
Applications that use the JSON-oriented query language, created by MongoDB, can interact with data stored in Informix® databases. The Informix database server also provides built-in JSON and BSON (binary JSON) data types.
应用程序使用JSON-oriented查询语言,由MongoDB,可以与Informix®数据库中存储的数据交互。Informix数据库服务器还提供内置的JSON和BSON(二进制JSON)数据类型。
You can use MongoDB community drivers to insert, update, and query JSON documents in Informix.
可以使用MongoDB社区驱动程序在Informix中插入、更新和查询JSON文档。
Not sure, but I believe you can use the Innovator-C edition (free for production) to test and use it with no-cost either for production enviroment.
不确定,但是我相信您可以使用Innovator-C edition(免费用于生产)来测试和使用它,而且对于生产环境都是免费的。