XML or Database Table to store settings

2019-07-04 04:34发布

问题:

I am about to start writing a site in PHP using MySQL as back-end. One of the first decisions that I have to make is whether to store site settings in a database table or in a XML file. I thought about storing them as key/value pairs, no matter which storage I use. It's probably the most flexible and best way to do it.

Since both ways are relatively simple to implement and use, my worries are about performance. Which one is faster ? Which one should I use ? Why ?

PS: How easy would it be to store the settings in an .ini file ? Can they be changed easily by a PHP script rather than manually by the user/sysadmin ?

Also, if I would store the settings in a mysql table, the sysadmin can easily cache that (very small) table in RAM. Can he do that with an XML file ?

回答1:

I assume the configuration is per user. Firstly I would suggest you to write a wrapper class which will access the configuration. That way you could only change the core of configuration in one class not in all code.

class configuration {
  public function get_user_configuration() {
    // return users configuration
  }
}

I think it would be easier to store configuration in database. Because then the configuration probably will be backed up daily and can be easily exported.

Caching this configuration is a good idea. The easiest way would be to cache it in session. You could also try memcache or APC.



回答2:

In any case, storing it in the DB makes sense. You can still cache it to a (XML) file when settings have changed if you want and load that file instead. So the application will never query the DB for the settings.