What function does Drupal provide to save an array in a file? I am writing a module that handles a large associative array which has string keys and floats values. 

$array = array('hello' =&gt; 122.23, 'name' =&gt; 334.3, /* etc... */);


I was thinking of storing it in the database by serializing it, but the array size is dynamic, so I thought of storing it in a file to reduce access time.

What functions should I use to create the file, write the content, and read the array from the file?
Apart from PHP functions for reading/saving data in files, Drupal 7 has file interface functions too; one of those is file_unmanaged_save_data() which saves data in a file. The difference between those Drupal functions, and the equivalent PHP functions is that the Drupal functions can handle the case the module is trying to save the content in a file that already exists. See FILE_EXISTS_ERROR, FILE_EXISTS_RENAME, and FILE_EXISTS_REPLACE.  

There are file interface functions that save a reference to the file created in a table. This can be useful in the cases the files needs to be automatically deleted because they are temporary, or programmatically deleted. See for example file_copy(), and file_move().

About using a database to save serialized data, that is what Drupal already does with the table containing the value of Drupal variables defined from modules. Its database table is defined as follow. (I show the definition used from Drupal 7, but Drupal 6 uses a similar definition.)

  $schema['variable'] = array(
    'description' =&gt; 'Named variable/value pairs created by Drupal core or any other module or theme. All variables are cached in memory at the start of every Drupal request so developers should not be careless about what is stored here.', 
    'fields' =&gt; array(
      'name' =&gt; array(
        'description' =&gt; 'The name of the variable.', 
        'type' =&gt; 'varchar', 
        'length' =&gt; 128, 
        'not null' =&gt; TRUE, 
        'default' =&gt; '',
      ), 
      'value' =&gt; array(
        'description' =&gt; 'The value of the variable.', 
        'type' =&gt; 'blob', 
        'not null' =&gt; TRUE, 
        'size' =&gt; 'big', 
        'translatable' =&gt; TRUE,
      ),
    ), 
    'primary key' =&gt; array('name'),
  );


During bootstrap, Drupal loads all the variables from the database, and saves their values in a global variable. This is the code used by Drupal for this purpose. (The function is variable_initialize(); the Drupal 6 function is variable_init(), called in the same circumstances.)

  // NOTE: caching the variables improves performance by 20% when serving
  // cached pages.
  if ($cached = cache_get('variables', 'cache_bootstrap')) {
    $variables = $cached-&gt;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}')-&gt;fetchAllKeyed());
      cache_set('variables', $variables, 'cache_bootstrap');
      lock_release($name);
    }
  }

  foreach ($conf as $name =&gt; $value) {
    $variables[$name] = $value;
  }

  return $variables;


Drupal doesn't have performance issues, even if that function is called for every rendered page. As you can see, it caches the value of the variables in a cache. If your data is requested frequently enough, you could consider using a cache table too.

As for using persistent variables to store data, keep in mind they are loaded in memory every time Drupal bootstrap; if you are going to store data that could not be used for the currently loaded page, then persistent variables are not what you should use for storing your data. Also, since variable_set() clears the cache used for the persistent variables, it is generally preferable to reduce the number of calls to variable_set() done for a single page.