如何使 PHP 脚本实时响应数据库插入?

How to make a PHP script react in real time to a database insertion?

我有多个 PHP 脚本 运行 24/7,每个都在不同的机器上,从 Internet 获取有关用户的信息。

这些用户在 (postgresql) 数据库的 table 中。每次新用户在应用程序中注册时,都会在 table 中插入一条新记录,并将其分配给当前 运行 PHP 脚本之一(平均分配负载)。

有没有办法让分配给它的 PHP 脚本可以实时做出反应,以便立即开始获取有关用户的信息?

(我的当前设置是每个 PHP 脚本每 1 分钟查询一次 table,并检查是否已将新用户分配给它...但是更频繁的查询会增加数据库加载,在那 1 分钟内 window 我没有从新添加的用户那里获取信息。)

检查 postgres LISTENNOTIFY 的 PHP 代码示例:http://php.net/manual/en/function.pg-get-notify.php

LISTEN/NOTIFY 上的 Postgres 人: http://www.postgresql.org/docs/current/static/sql-notify.html http://www.postgresql.org/docs/current/static/sql-listen.html

通过 SQL 触发器sys_exec() 启动触发 HTTP 请求?但这似乎有点过分了。

DELIMITER $$

CREATE TRIGGER `test_after_insert` AFTER INSERT ON `test`
FOR EACH ROW BEGIN

SET @exec_var = sys_exec('/usr/bin/php /myApp/after_insert.php')
END;
$$

DELIMITER ;

我知道这不是 postgres 脚本,但它应该可以工作。

--- 编辑 ---

after_insert.php 之后:

function httpPost($url, $data)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

来源: