C
C
Counter2018-02-07 15:58:24
PHP
Counter, 2018-02-07 15:58:24

What is the correct way to save execution state?

Good afternoon.
There is a script that is executed by a web hook from another service and does certain actions. A user in a pair can send this webhook with the same parameters 2 or 3 times in a row in a second. And this script can be executed only once with the same webhook parameter, all other attempts must be rejected. It is necessary to bind to the webhook id, since 2 different webhooks may arrive one after another and they will need to be executed, or 3 identical ones may arrive. 2 of which will need to be rejected.
2 options come to mind:
1. Write the state to mysql and check this state with a query before execution.
2. Write the state to the session and also check before execution.
It is enough to save the state for 5 minutes, then it is no longer needed.
Can you please tell me how to implement this correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Lysenko, 2018-02-07
@Counter

For such purposes, it is very convenient to use memcache, set the value by a unique key, for example MY_KEY_ . id_user with TTL for 5 minutes, before executing, check if there is such a value in the cache. After the ttl expires, the cache will kill this value itself. It is very inexpedient and resource-intensive to pull the database for such things, especially if the project is under load.

A
Andreo, 2018-02-07
@chupacabramiamor

3. Lock the button on the front, which sends your data via a hook, via JavaScript
4. In addition to point 3 - although not necessary, you can use a token in the form that is invalidated after your request (CSRF)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question