WordPress 多个 ajax post 请求,get_option 不显示后续 ajax post 请求的更改

发布于 2025-01-11 23:42:24 字数 998 浏览 0 评论 0原文

我有一个长期运行的发布请求处理程序,可以批量处理项目。我希望用户能够通过发送另一个发布请求随时取消此批次。我有一个存储在 wp_options 中的标志,但是,后面的取消请求似乎无法被第一个长期存在的发布请求处理程序读取。

示例:

add_action('wp_ajax_batch', 'my_ajax_batch_handler');

function my_ajax_batch_handler()
{
  update_option('batch_state', 'running');
  // process items one by one, if the batch_state is ever 'cancelled', then quit.
  foreach ($values as $item) {
      // $batch_state is never 'cancelled' here, always 'running' even when it has been 
      // set to 'cancelled' in the following function.
      $batch_state = get_option('batch_state');
      if (!batch_state or batch_state == 'cancelled') {
         break;
      }
  }
}

add_action('wp_ajax_cancel_batch', 'my_ajax_cancel_batch_handler');

function my_ajax_cancel_batch_handler()
{
  update_option('batch_state', 'cancelled');
  // batch_state is cancelled here, but this change does not reflect in the
  // loop in the previous function.
}

Wordpress/php 是否为每个 ajax post 请求提供隔离的环境?

I have a long-running post request handler that batch processes items. I want the user to be able to cancel this batch at any time by sending another post request. I have a flag that is stored in wp_options, however, the later cancel request does not appear to be able to be read by the first long-lived post request handler.

Example:

add_action('wp_ajax_batch', 'my_ajax_batch_handler');

function my_ajax_batch_handler()
{
  update_option('batch_state', 'running');
  // process items one by one, if the batch_state is ever 'cancelled', then quit.
  foreach ($values as $item) {
      // $batch_state is never 'cancelled' here, always 'running' even when it has been 
      // set to 'cancelled' in the following function.
      $batch_state = get_option('batch_state');
      if (!batch_state or batch_state == 'cancelled') {
         break;
      }
  }
}

add_action('wp_ajax_cancel_batch', 'my_ajax_cancel_batch_handler');

function my_ajax_cancel_batch_handler()
{
  update_option('batch_state', 'cancelled');
  // batch_state is cancelled here, but this change does not reflect in the
  // loop in the previous function.
}

Does Wordpress/php have isolated environments for each ajax post request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-01-18 23:42:24

我最终通过在循环中调用 get_option 之前清除缓存来解决这个问题。

add_action('wp_ajax_batch', 'my_ajax_batch_handler');

function my_ajax_batch_handler()
{
  update_option('batch_state', 'running');
  foreach ($values as $item) {
      // clear cache
      wp_cache_flush();
      wp_cache_delete('batch_state');
      $batch_state = get_option('batch_state');
      if (!batch_state or batch_state == 'cancelled') {
         break;
      }
  }
}

不确定为什么存在这种行为,但清除缓存可以解决它。

I ended up solving this issue by clearing the cache before the get_option call in the loop.

add_action('wp_ajax_batch', 'my_ajax_batch_handler');

function my_ajax_batch_handler()
{
  update_option('batch_state', 'running');
  foreach ($values as $item) {
      // clear cache
      wp_cache_flush();
      wp_cache_delete('batch_state');
      $batch_state = get_option('batch_state');
      if (!batch_state or batch_state == 'cancelled') {
         break;
      }
  }
}

Not sure why this behavior exists, but clearing the cache solves it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文