[nycphp-talk] APC for a custom PHP session handler
John Campbell
jcampbell1 at gmail.com
Wed Mar 25 02:41:43 EDT 2009
On Wed, Mar 25, 2009 at 12:03 AM, Steve Manes <smanes at magpie.com> wrote:
> John Campbell wrote:
>>
>> Don't do it. It is either a solution to a problem you don't have, or
>> the wrong solution. APC has one datastore per server, which will be a
>> disaster once you have more than 1 front end machine, or if you have
>> to restart the webserver then all your users will get logged out.
>
> I'm aware that it's a single server solution. So is the stock /tmp session
> storage. However most web sites are single server and are unlikely ever to
> be clustered so there's definitely a "market" for it. Worst case, since
> PHP's session support is so well partitioned, if you should need to cluster
> later you can always return to a database solution for session support
> without affecting your application code.
>
> I'm just floating an idea here. If you're already employing APC, using its
> user cache space for session data store would save one round trip to the
> database per session-targeted page request, which isn't insignificant.
>
> I'm just wondering if there are some other reasons for not using APC for
> this.
Assuming a single server solution, you are trading persistence for
performance. IMO, the performance gain is trivial unless you are
constantly modifying your sessions.
I use a trick that eliminates 95% of session related disk I/O, and
relies on two principles:
1) Only write the session data if it has actually changed.
2) Update the access time on read, but allow for a window (e.g.
Sessions last 5 hours, but are only extended if they are accessed when
0-2 hours remains on the session)
Below is some pseudo code:
$sh = new Custom_Session_Handler($db_link,$min_lifetime,$max_lifetime)
class Custom_Session_Handler {
function _read($id) {
// normal read stuff
if($valid_until < time() - $this->min_lifetime ) {
UPDATE sessions SET valid_until = NOW() + $this->max_lifetime
}
$this->_data = $data;
return $data;
}
function _write($data) {
if ($data != $this->_data) {
// update sql
}
}
}
Regards,
John Campbell
More information about the talk
mailing list