Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session overload - what is "too much data" stored in a session in PHP?

I'm using the session array to cache chunks of information retrieved from the db:

$result = mysql_query('select * from table');
array_push($_SESSION['data'],new Data(mysql_fetch_assoc($result)));

My question is, is there a limit/a sizeable amount of information that can/should be passed around in a session? Is it ill advised or significantly performance hindering to do this?

like image 897
Some1PHP Avatar asked Feb 06 '10 18:02

Some1PHP


2 Answers

By default, $_SESSION data is stored on disk in the /tmp directory of your server. As long as you have enough room in there AND you aren't hitting your PHP memory limit, you're fine.

However, if you're attempting to cache a query that is the SAME for a larger number of users, you might want to use something like APC or memcache that isn't tied to the individual user. Otherwise, your essentially going to cache the same result 1x for each user, and not leveraging a cache across all users.

like image 80
Mike Sherov Avatar answered Nov 15 '22 06:11

Mike Sherov


I think the answer would depend on where you are storing your data and how fast you can transfer it there.

If the data is 44 MB big, and you are on a 1000base-T network, you can expect it to take 1 second to actually transfer THERE. And 1 second to transfer back..

If you use local memory, then you have a finite amount of memory the machine.

If you use disk, then you have load/save times (disk is slow).

But also keep in mind, PHP has a finite amount of memory it allows a script to use. I think the default setting is 8 MB.

If you are talking about large blocks of data, you may want to consider Redis, Tokyo Cabinet or other key/value stores. Or even a backend interface to manipulate the data/cache it for you without transferring it through PHP.

like image 39
Daniel Avatar answered Nov 15 '22 07:11

Daniel