2

I have a table in my Drupal database this is simply a name value pair for each entry. I need to write to this table from javascript and get back as soon as possible. My first attempt was to create a menu endpoint that looks like this:

  $name = $_POST['propertyName'];
  $value = $_POST['propertyValue'];
  $result = db_query('SELECT * FROM {global_properties} WHERE property=:property', array(':property' => $name))->fetchAll();
  $record = array(
    "property" => $name,
    "value" => $value
    );
  $primary_key = !empty($result) ? array('property') : NULL;
  drupal_write_record('global_properties', $record, $primary_key);

I hit this endpoint from JavaScript using a dojo.xhrPost.

Sadly speed is quite important for this operation and it is taking me around 1000ms to get back from my post (I would like to be around 100ms). What would be a faster way to write to the Drupal table?

I am running this page on a server running MAMP Pro.

3

If you have zero concerns about user verification and data validation (seems to be the case given the example code) then I would post to a custom script that just boots up the database and does the write. Note that drupal_write_record is only available at the full bootstrap level.

Example code below:

/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());

// Boot up Drupal.
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

$name = $_POST['propertyName'];
$value = $_POST['propertyValue'];
$result = db_query('SELECT * FROM {global_properties} WHERE property=:property', array(':property' => $name))->fetchAll();
$record = array(
  "property" => $name,
  "value" => $value
  );
$primary_key = !empty($result) ? array('property') : NULL;

// Load up code for drupal_write_record().
// Untested and might break stuff as we are not at a full bootstrap.
require_once DRUPAL_ROOT . '/includes/common.inc';
drupal_write_record('global_properties', $record, $primary_key);

If you do care about user verification and server side data validation and thus need a full drupal bootstrap but the client does not care about what is returned then httprl's httprl_background_processing() function might do what you need. Example of this workflow below.

/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());

// Cut The Connection.
require_once DRUPAL_ROOT . '/sites/all/modules/httprl/httprl.module';
httprl_background_processing();

// Fully Boot Up Drupal.
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Do various user checks here.

// Run Example code.
$name = $_POST['propertyName'];
$value = $_POST['propertyValue'];
$result = db_query('SELECT * FROM {global_properties} WHERE property=:property', array(':property' => $name))->fetchAll();
$record = array(
  "property" => $name,
  "value" => $value
  );
$primary_key = !empty($result) ? array('property') : NULL;

drupal_write_record('global_properties', $record, $primary_key);

What the above example will do is return ASAP to Apache but drupal/php will run in the background. If the above example doesn't work there is one other option using HTTPRL in order to get a background worker running.

1
  • Thank you so much for your answer. This got it down to 25ms which is great. Currently we are not worried user verification or data validation but its good to have the second part of your answer for when that becomes an issue.
    – Mike2012
    Feb 28 '13 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.