views:

349

answers:

5

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?

A: 

Because Session data is stored in a file (or database record) on your server, it shouldn't matter too much how much data you store in it. I would just advise against huge objects.

You might want to look at APC or memcached to cache the results instead, as it is not a per-user cache, and it uses the memory instead of files.

Chacha102
A: 

The session is serialized and written to disk by default, so depending on the size and the amount of users things can become slow. However both things can be changed (read the session manual under http://php.net/session for all details) like using memcache for in-memory storage of the data. Best thing is to try it out under an environment as similar as possible tothe live system and check the resulting load and throughput.

johannes
A: 

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 megs 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 megs.

If you are talking about large blocks of data, you may want to consider redis, tokyocabnit or other key/value stores. Or even a backend interface to manipulate the data/cache it for you without transferring it though php

Daniel
+2  A: 

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.

Mike Sherov
A: 

mmm tricky. I think you could save it in the session. The real question is ¿do you want that all that information serialize and unserialize every time a client make a request? . I think it would be ok to save it in there if you will use all that information in every page of your website, but this is unprobable. It would be better if you save that information in a directory like /temptables/sometable/ and each file have the name of the session. you can use session_id to get it, and save and load the information in the pages you have to use with:

$info = unserialize(file_get_contents('/templatebles/sometable/'.session_id().'.ser'));

and saving with:

file_put_contents('/temptables/sometable/'.session_id().'.ser'), serialize($info));

But you need a cron job to clean that directory for old file. you can do it getting the session from the filename and ask for some variable like 'itsalive' using session_start() or doing something like file_exists(session_save_path().'/sess_'.$session_name) to check if you should delete the temporary file.

useless