Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Memcache(d) based sessions : Should garbage collection be disabled?

When using the pecl memcached (or memcache I guess..) extension, should php's session garbage collection (eg: session.gc_probability / session.gc_divisor) be disabled by setting the probability to 0?

It seams as tho this might be logical for the following reasons:

A) Session expiry time is most likely simply stored by setting the expiry time on the stored key. EG: Each session is given its expiry ttl, and simply purged by memcached when it expires.

B) To flush out existing sessions that have not been purged by memcached itself, the memcached extension would have to do a full dump of all the data that is stored in the memcache daemon, check each key to see if the key matches the defined pattern, and then check when the key was added, and finally remove as necessary. This is unlikely for the following reasons: 1) As far as I know, there is no to tell when a key was added to memcache. Only when it expires. 2) The dump + parse would be super heavy on memcache instance.

Then again, its quite possible that the memcache(d) pecl extension simply disables php's session garbage collection?

Thanks.

like image 931
anonymous-one Avatar asked Feb 19 '23 08:02

anonymous-one


1 Answers

The PHP ini setting session.gc_maxlifetime defines the expiration of a session entry. If you disable this option your session will be active forever.

If you have a look at the Memcached sources it’s easy to spot the behavior (even if you don’t understand C): https://github.com/php-memcached-dev/php-memcached/blob/e781e169871fd4f14f844ce3e01860e84ec28831/php_memcached_session.c#L325-L327

The expiration is set to 0 by default and only set if the PHP ini setting session.gc_maxlifetime is set to something greater than 0. Just because the setting contains a gc doesn’t say that this has something to do with PHPs session garbage collection. The Memcached extension simply interprets this option as it can utilize it. No PHP garbage collection is necessary if you use Memcached, as it will clean itself and the garbage collection is disabled by the extension itself.

But you still have to set the setting!

like image 118
Fleshgrinder Avatar answered Mar 15 '23 22:03

Fleshgrinder