问题描述
是否只是想知道是否有人拥有一个用于在php中存储/更新动态设置的良好系统?
Was just wondering if anyone had a good system for storing / updating dynamic settings in php?
动态是指其他脚本会自动更改的设置.
What i mean by dynamic is settings that change automatically by other scripts.
当前,我们使用的系统基本上是file_get_contents我们的设置php文件,将其视为字符串,将设置更改为file_put_contents更新后的设置.
Currently we use a system that basically file_get_contents our settings php file, treats that as a string, changes settings and file_put_contents the updated settings.
它可以工作,但是有点脏.还有其他建议可以在其中存储/修改/提取这些设置吗?不能使用Mysql,我们希望避免多余的查询.
It works but its a bit dirty. Any other recommendations where we can store / modify / pull these settings from? Mysql is NOT an option, we want to avoid the extra queries.
作为一项额外的奖励,某些系统可以在多个服务器上共享此数据,因为跨多个设备使用了许多这样的设置.但我想我们可以分开处理.
As an added bonus, some system that can share this data over multiple servers as many of these settings are used across multiple boxes. But i suppose we can deal with that seperatly.
谢谢!
推荐答案
如果您坚持使用基于文件的方法,请记住以下几点:
Things to keep in mind if you stick with a file-based approach:
用C编写的解析器最有可能比PHP中的解析器要快.我想到的两个选择是
A parser written in C will most likely be quicker than one in PHP.The two options that spring to mind are
使用一个settings.php
文件,其中包含格式正确的PHP代码.我过去使用的一种方法是拥有一个看起来像
Use a settings.php
file, that contains well-formed PHP code. One approach I've used in the past is having a file that looks like
<?php return
array(
'key' => 'value'
);
此文件可以是 include
-d一个或更多的时间,并且include
语句将返回配置数组.在编写新设置时,请使用 var_export()
获取有效的php代码.
This file can be include
-d one or more times, and the include
statement will return the configuration array. When you're writing the new settings, use var_export()
to get valid php code.
另一种方法是使用settings.csv
文件.使用 fgetcsv()
阅读,使用 fputcsv()
.
The other approach is using a settings.csv
file. Read using fgetcsv()
, write using fputcsv()
.
为避免在更新设置时读取部分文件,请首先将新设置写入临时文件,然后将该文件移动到设置文件位置(最好使用* nix上的mv (1)
).
To avoid a partial file being read while the settings are being updated, first write the new settings to a temporary file, then move this file to the setting file location (preferrably using mv (1)
on *nix).
这篇关于在php中存储动态设置的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!