A
A
Alexey Nikolaev2014-10-03 19:07:58
PHP
Alexey Nikolaev, 2014-10-03 19:07:58

Is it possible to implement a self-cleaning table in mysql, or how to catch the session expiration moment?

I want to implement a kind of self-cleaning table, from which all records with sessid = id of the expired session on this machine will be deleted. Or make it so that when the row limit is reached, old rows are pushed out and new records take their place.
I started looking towards memory, but this is absolutely not what you need. It is possible to implement such a bicycle from dozens of lines of php code, but here the question arises of how to catch (without checking by cron) the session expiration. And maybe there are more efficient and simpler ways (for example, mysql triggers ) than creating a bicycle from tens (if not hundreds) of lines of code?
I would be grateful for advice and recommendations, thanks.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex Chistyakov, 2014-10-04
@Heian

Don't store session information in MySQL, store it in a storage where you can set the lifetime of the record when you set the value.
For example in Redis .

K
Konstantin Andreevich, 2014-10-03
@reffy

What does tens-hundreds of lines of code mean? And the question is "how to catch session expiration"? One way or another, at a certain moment you request a session from the database, and check whether it has expired or not, at that very moment, this is completely cheap in terms of resources and is solved by a line of code.
Well, since you are fighting so hard for the cleanliness of the database that you are ready to delete sessions, that cron is your enemy. Either you don’t clean them at all and they accumulate in megabytes, or you get hung up on this for nothing.
And who prevents to make the user (let's say the users table) the sid field (in which the id of the current session is stored). And sid in this case is a link to another table, where all the information on the session is already available, and when everything else was created. There's really nothing to clean here.

A
Alexander Kubintsev, 2014-10-03
@akubintsev

Write a cron task to clear entries in the session table older than a given constant.

L
Lemfidavr, 2014-10-04
@Lemfidavr

With triggers, it's unlikely that it will work.
As mentioned above, you can organize using cron.
Through PHP to set session lifetime.
I don't see any better solutions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question