Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

less expensive to use local arrays rather than always SESSION? 1

Status
Not open for further replies.

williez

MIS
Apr 2, 2004
40
US
Is it less expensive to copy a multi-dim array from SESSION to a local variable, make changes (sometimes many) and then write the whole array back to SESSION, or is there no 'savings' and writing to SESSION constantly is just as good?

My one-page php program uses from from 3 to 10 dimensions deep which are foreach'ed as well as directly accessed.
I.E.: $_SESSION['undr'][$und]['vshft']['dsp'][$cp]['exp'][$exp]['mtxlnk']

My reasoning tells me (with very limited experience to draw upon) that SESSION info is saved in cookies which are on disk, so referring to disk for every sub-dim change is more expensive than copying a branch to a local variable, changing it, then when done, copying it back.

$vshft = $_SESSION['undr'][$und]['vshft'];
$vshft['dsp'][$cp]['exp'][$exp]['mtxlnk'] = 7;
$vshft['prn'][$cp]['tot'] = 4.332;
.
.
$_SESSION['undr'][$und]['vshft'] = $vshft;

I'm looking to minimize external impacts, so I take my oracle-query results and stuff them into arrays like this and _never_ re-query for the same thing (unless its dependencies have changed).

(Any comments on my never re-querying is appreciated as well.)

Thank you,
Will
 
session data is not saved in cookies. cookies are client side and sessions are server side.

if php is using sessions to propagate data then the client side will receive a cookie with just the corresponding session id.

php uses this info to restore the session each page call.

within a script, session data is stored in the same way as any other array with global scope. the extra 'effort' involved in a session array is that php will serialize and store to disk the session data at the end of each script. usually the data has already been sent to a browser by this time. Equally, when a session is started, php goes to find the current session data and deserializes it.

as to whether it is a good thing to cache query results, in general it's ok provided you don't retrieve data that you don't absolutely need. Personally, for just about every database call I do, I make it fresh as the latency involved is not significant. Conversely, if you are making queries that have material latency involved (megabytes of data) then I would definitely recommend NOT cacheing the data as you can hit memory limits in php (changeable in some instances). The advantage of a database connection in such instances is that you can iterate over the dataset without having to load the whole dataset into memory.
 
Thanks for the reality check. If I understand correctly...

The client has a cookie with the session id in it. The server gets the session-id and then knows which saved data to use running the PHP script. the difference between SESSION-variables and local-variable is the persistence of SESSION-variables. So copying a subset of the multi-dim wastes the additional memory needed for the duplicate variables (but it also makes the variables shorter in the php code).

As for loading db results into variable arrays, I guess saving the essential info (the constraints that are needed for the content-query) into variable-arrays and directly outputting the result of the content-queries is the 'least expensive' balance between needing lots of memory and re-querying the database. (Using memory is only faster if you have enough of it.)

Thanks for the reply jpadie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top