zoukankan      html  css  js  c++  java
  • Drupal如何处理系统变量?

    Drupal的系统变量都保存在数据库variable表中:

    然后,开发人员可以通过下面的API函数操作这些系统变量:

    function variable_get($name, $default = NULL) {
      global $conf;
      return isset($conf[$name]) ? $conf[$name] : $default;
    }
    
    function variable_set($name, $value) {
      global $conf;
      db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
      cache_clear_all('variables', 'cache_bootstrap');
      $conf[$name] = $value;
    }
    
    function variable_del($name) {
      global $conf;
      db_delete('variable')
        ->condition('name', $name)
        ->execute();
      cache_clear_all('variables', 'cache_bootstrap');
      unset($conf[$name]);
    }

    从这些API函数可以知道,系统变量从variable表读取出来后,都放在了全局变量$conf里面。那全局变量$conf是如何初始化的呢?这实际上是通过在Bootstrap过程中调用variable_initialize()函数完成的:

    function variable_initialize($conf = array()) {
      if ($cached = cache_get('variables', 'cache_bootstrap')) {
        $variables = $cached->data;
      }
      else {
        // Cache miss. Avoid a stampede.
        $name = 'variable_init';
        if (!lock_acquire($name, 1)) {
          // Another request is building the variable cache.
          // Wait, then re-run this function.
          lock_wait($name);
          return variable_initialize($conf);
        }
        else {
          // Proceed with variable rebuild.
          $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
          cache_set('variables', $variables, 'cache_bootstrap');
          lock_release($name);
        }
      }
    
      foreach ($conf as $name => $value) {
        $variables[$name] = $value;
      }
    
      return $variables;
    }
  • 相关阅读:
    HashSet集合保证元素唯一性的原理 底层代码 ---有用
    集合(下) ---有用
    集合(上) Colection方法 并发修改异常
    Hive -- Hive面试题及答案sql语句 ---阿善有时间看
    hive面试题总结(2020最新版) hive优化方面 ---阿善重要
    hive面试题总结(2020最新版)
    Hive常见面试题1.0
    Hive面试题收集 ---阿善重要
    promise的三个缺点
    js中常见的高阶函数
  • 原文地址:https://www.cnblogs.com/eastson/p/3355580.html
Copyright © 2011-2022 走看看