Question

Do PHP static variables, which do persist during a normal page request, persist between progressive Batch API runs? What about during non-progressive (command-line) batch execution? Does &drupal_static come into play in any way?

In my application, I want to take advantage of in-memory caching across batch processes.

Was it helpful?

Solution

No they do not.

I had a similar question some time ago: https://drupal.stackexchange.com/a/59274/1899

PHP static variables do not persist between requests.

In your batch operation, you receive an array argument as the last argument. Call it &$context (pass by reference), and then you can have persistent variables.

  • $context['sandbox'] preserved between iterations of the same batch task. Useful for progressive batch OPs.

  • $context['results'] preserved throughout the entire batch progress.

OTHER TIPS

Static variables exist per request, so no, they won't persist across separate batch runs.

If you need to cache data across requests in memory just use redis/memcache/etc, there are contrib modules available for each that hook into the core caching stays so you can use a familiar API

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top