2

cache_get() is not working as expected when called in a request initiated by drupal_http_request() on the same server. Is it true that the cache_set() immediately before drupal_http_request() doesn't actually take place in the database until the first database transaction is committed?

Given the sequence:

  • cache_set($cid, $data)
  • cache_get($cid) // returns data
  • external request using drupal_http_request()
    • cache_get($cid) // same $cid, returns FALSE
  • cache_get($cid) // of course, returns data

To make the cached item available to the request initiated by drupal_http_request() I wrapped the cache_set call in a function like this, but it had no effect.

function cache_set_wrapper($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
  $txn = db_transaction();
  return cache_set($cid, $data, $bin, $expire);
}

Why is there a discrepancy in the cache between the initial request on the second request?

  • 1
    That depends on where and how you are calling cache_set(). A transaction only exists if someone explicitly started it. If you're in a node, user or other entity insert/update hook for example, then yes, then you are in a transaction. – Berdir Jul 26 '14 at 21:49
  • Very good. This is from hook_node_insert(). So my transaction is started after the one for the node. Mine goes out of scope first, but does it get executed before the node_insert transaction is executed? – htoip Jul 26 '14 at 21:59
  • No, if there is an active transaction that it will only get committed at the end. Nested transactions are supported but only to rollback to a certain step. Does the external request need to happen immediately? Doing external requests from a entity crud hook is always a bit problematic for performance (imagine a bulk resave or import of nodes). Consider moving the http request to a shutdown function or a queue. – Berdir Jul 27 '14 at 11:24
  • Thanks. You've been incredibly helpful. The external request doesn't need to happen immediately, so I'll see about using a queue. – htoip Jul 28 '14 at 3:03
  • Can you put your comments into an answer? It's exactly what I wanted to know. – htoip Jul 28 '14 at 3:04
2

Copied from my comments:

That depends on where and how you are calling cache_set(). A transaction only exists if someone explicitly started it. If you're in a node, user or other entity insert/update hook for example, then yes, then you are in a transaction.

If there is an active transaction that it will only get committed at the end. Nested transactions are supported but only to rollback to a certain step. Does the external request need to happen immediately? Doing external requests from a entity crud hook is always a bit problematic for performance (imagine a bulk resave or import of nodes) too. Consider moving the HTTP request to a shutdown function or a queue.

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.