昨天群里有个朋友问有关session生命周期如何设置的问题。本人也没做过高负载的项目 所以一般都是用memcache存储session便于控制.所以对php 处理session的方案也没有过多了解,就随口就回答了个gc_maxlifetime.
因为php.ini里关于此参数的注释是
; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
看了下也没问题“超过设置的秒数,则储存的数据将被垃圾清理进程处理掉。”
实际在我将gc_maxlifetime 设置成5秒以后 着手测试了一下 发现情况并不是这么简单
首先.关于garbage collection的运行还有一个几率问题.
就是在 gc_maxlifetime上面的两个参数gc_probability和gc_divisor
php官方的注释是
; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.
可以看出这个比例决定了 GC process 的启动几率
然后我将这两个值都设置为1,即100%几率触发GC 然后设置session 过了5秒后
注释第二行,再刷新页面获取session