1

variable_get seems to always used $conf array which is fetched a single time. In a long running process where the value of a variable may have changed during operations this causes problems. Is there a simple mechanism in Drupal to tell variable_get to refetch the array from the database rather than using the in-memory variable?

7
  • if you use variable_set() to modify the variable then variable_get() will always give you what you expect. Commented Jul 22, 2014 at 15:17
  • @ScottJoudry Not necessarily; it can be overridden in settings.php, and (as the OP is hinting at), can be changed in memory during a single request. ..
    – Clive
    Commented Jul 22, 2014 at 15:23
  • What problems are you having? When variable_set() executes, it updates the database, clears the cached config, and then updates the global $conf.
    – mpdonadio
    Commented Jul 22, 2014 at 15:36
  • @ScottJoudry the problem is that the process that is doing the variable_get is started BEFORE the variable_set is called. The variable_set updates the database but since the process is already running, the $conf array in memory for that process isn't re-fetched so variable_get always uses the one in memory.
    – Oddible
    Commented Jul 22, 2014 at 15:37
  • I can't think of a situation where I would set a variable using variable_set and then use variable_get to retrieve the value I just set. You have the value in a variable already when you run variable_set, why not just use that? Commented Jul 22, 2014 at 15:43

1 Answer 1

1

I don't think Drupal core has anything like that, no. For speed and efficiency, the variables array is in memory pretty much as soon as settings.php has been processed.

You've always got db_query():

$var = db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'var_name'))->fetchField();
$var = unserialize($var);

It would be trivial to build up an efficient variable_get_from_db function based on that.

5
  • This is the answer I expected, was holding out that something like this was already part of the core.
    – Oddible
    Commented Jul 22, 2014 at 15:38
  • Yeah I think you're stuck with something manual to be honest, the variables code is pretty bare bones and there doesn't seem to be anything to cover this use case
    – Clive
    Commented Jul 22, 2014 at 15:39
  • This just sounds like a bad usage of variable get/set a long running application should use a static local variable or some other custom variable store like it's own DB table for this kind of logic. I dont think blaming the variable api is necessarily right here. I believe the Migrate module does this with its statuses.
    – tenken
    Commented Jul 22, 2014 at 15:42
  • @tenken Agreed, but it's not uncommon for variables to be (rightly or wrongly) used for state in Drupal 7 and lower. D8 has the new state API of course, so makes things a lot cleaner. The big question is whether this variable needs to persist beyond a single page load. If the answer is no, variables are not the answer. But if it's yes, they arguably could be. Although yeah, I'd rather use a session even for that but that's just my opinion...
    – Clive
    Commented Jul 22, 2014 at 15:45
  • This is for a situation similar to migrate in that it is a long running process and I just want a persistent variable to cancel it if needed. So periodically it checks an 'am I cancelled' variable and if that returns true I gracefully exit the process. I can set the 'am I cancelled' variable via the admin page for the app. But the process doesn't have its own database table for anything else so setting one up just for a single cancel variable is so overkill, was hoping to use Drupal's existing persistent variable storage but it seems to not have that functionality.
    – Oddible
    Commented Jul 22, 2014 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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