简体   繁体   中英

php redis session save handler extending function gc (garbage collection)

Is there a way to extend phpredis session.save handler to call a function when garbage collection happens?

        ini_set('session.save_handler','redis');
        //code to set an additional gc function
        session_start();

I am looking to add an additional cleanup step for my session. The data I want to cleanup are temporary files in a database.

If it isn't possible to extend phpredis would there be a way to write a function that uses to simulate session garbage collection with the below ini settings?

session.gc_probability = 1
session.gc_divisor = 100

Here is what I came up with

$gc_probability = ini_get('session.gc_probability');
$gc_divisor = ini_get('session.gc_divisor');
$probability = $gc_probability/$gc_divisor;
$random_float_between_0_and_1 = mt_rand() / mt_getrandmax();
        
if ($random_float_between_0_and_1 <= $probability)
{
    $this->cleanup_expired_files();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM