如题,数据库用的是MySQL。其中某个表一直以周期性添加一条数据。
目前用的是轮询的方式。
题主想知道数据库是否支持类似于Java中观察者模式,当数据库添加记录后,就会自动触发某个自定义函数?
高洛峰2017-04-17 11:30:29
The answers below are all based on the large amount of data in the main table.
(1) Use mysql trigger
(2) mysql can run external scripts (may be related to user permissions and platform), and trigger back-end programs through scripts
(1) Use mysql trigger
(2) After triggering the data, write the data to the memory table
(3) The back-end program polls the memory table. After the processing is completed, the polled data is deleted to prevent the memory table from exploding
PHP中文网2017-04-17 11:30:29
The best solution to your idea should be Hook
to make a hook for MySQL and execute the hook program when the database changes. However, after searching, there seems to be no good ideas for directly adding Hooks to the database.
The only feasible way is to use Git to manage your MySQL database, and call Hook through Git when the database file changes. Although it is feasible, it feels a little geeky.
So it seems that the simpler idea is to think from your program. When the program executes the insert()
function in the database operation class, it executes a certain Hook函数
. That is to say, do not add hooks to the database, but add hooks to the database operation class of the program.