doufei2662 2010-07-20 20:22
浏览 37
已采纳

存储网站偏好的最佳方法是什么?

I'm currently working on a massive revamp of my CMS, and was wondering, what would be the best way to store site preferences...

Currently my pereference setup is within database, full of structure fields (each representing it's own preference), but only id 1 is used. And... I somehow don't like this solution. Now I'm thinking of setting preferences within php file, but then there are problems with editing preferences. Well, not problems, but using files as storage is just bad in my opinion.

With site preferences I mean "Site title", "Site slogan", "Meta keywords", "Meta description" etc., not database info or other totally static values.

So... Are there any other solutions you guys could share?

Thanks in advance!

  • 写回答

3条回答 默认 最新

  • douyin2962 2010-07-20 20:53
    关注

    Well, there's no easy answer to this question. As @Redlab asked, are the preferences frequently changing? If so, then the DB seems like the "better" alternative. But there is a lot more to it than that...

    Are there a lot of these preferences? If so, then storing it in the filesystem may make sense with namespaced files and hiearchial storage. So for example:

    In site.php

    return array(
        'title' => 'This is my page title',
        'meta' => array(
            'description' => 'My Meta Description',
            'keywords' => 'key1, key2, key3',
        ),
    );
    

    Basically, create a file for each "namespace" to group things together.

    The advantage to doing it this way is performance and managability. You can lazy-load the config on a per-namespace basis as needed. You could mimic the effect in the database by adding a field called namespace, and then lazy-loading those fields, but it's going to be significantly slower since every access requires a query. If you have a lot of settings, then loading it from the db on every page hit (even once) is not going to be optimal either...

    What I do is use dot notation. So from the above example, to get the meta keywords would be site.meta.keywords. It's actually reasonably easy to do:

    class Config {
        protected $data = array();
    
        public function get($name, $default = null) {
            $parts = explode('.', $name);
            $ns = array_shift($parts);
            if (!isset($this->data[$ns])) {
                $this->loadNS($ns);
            }
            $search = $this->data[$ns];
            foreach ($parts as $part) {
                if (is_array($search) && isset($search[$part])) {
                    $search = $search[$part];
                } else {
                    return $default;
                }
            }
            return $search;
        }
    
        protected loadNS($name) {
            $path = PATH_TO_CONFIG . $name . '.php';
            if (is_file($path)) {
                $this->data[$name] = include($path);
            } else {
                $this->data[$name] = array();
            }
        }
    }
    

    You can add a set and a save method just as easy.

    To save the files:

    public function saveToFile($ns) {
        if (!isset($this->data[$ns])) {
            $this->loadNS($ns);
        }
        $out = '<?php return '.var_export($this->data[$ns], true).';';
        $path = PATH_TO_CONFIG . $ns . '.php'
        file_put_contents($path, $out);
    }
    

    What results is a very easy to maintain (since it's only ever writing to the filesystem) and easy to modify by hand (since it's all just php code) system for storing and retrieving information.

    And best of all, since it can take advantage of opcode caches, it's VERY efficient and won't slow your page down at all...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算