tags:

views:

93

answers:

3

Hi, I am programming in PHP mysql. I have recently got into OOP programming.

So I need to serialize my objects and store them to SESSIONS. However I think the huge sessions are slowing down refreshing and loading of my webpages.

Is there an alternative approach (than serializing deserializing) so that I can still share my objects from webpage to webpage and improve loading time.

Thanks Rahul

+1  A: 

Are all the objects you store necessary from page load to page load? If not then you need to keep those objects out of the session and only reconstruct them on the pages that you need them.

Every object you store in the session will get serialized and unserialized on every page load regardless if you actually need it. So you'll want to keep that to a minimum.

Another option is store only what you need to reconstruct the object in the session and not the entire object. For instance you can store a database id in the session and reconstruct the objects from the database when you need them.

smack0007
A: 

As a rule of thumb you should try to limit your Session-size to 4KB or less (independent on what programming language you use). If your data get's bigger than that, you should start using Tables in a Database (like MySQL/PostgreSQL/...) to persist the data.

Example: Store a draft for a Blog Article

  1. In the Session (with all it's images etc) vs
  2. In the Article-DB-Table (where a flag draft=1).

Believe me, it's more convenient if you choose the DB (and you don't have to hassle with serialization).

Marcel J.
Hi,Is there anyway to see the size of a session... like echo it. I dont have an idea of the sizes of my sessionsthanks
Rahul
If you are using Apache, take a look into your APACHE_ROOT/tmp folder and look for files named sess_*. Otherwise take the script from http://paste-it.net/public/f305f9c/ and pass it the $_SESSION.
Marcel J.
+1  A: 

You should first analyze what the actual bottleneck is. Is it really the object serialization/deserialization?

If so, ask yourself, if all the objects need to be present on every request or if they can be reconstructed on demand. Then you could just store the key values to reconstruct those objects instead of the whole objects.

But if you need all those objects though, use more performant storage location than the default (files in the file system), maybe the memory (memcache).

Gumbo
thanks, where can i find the settings for storage.
Rahul
@Rahul: Use either `session.save_handler` (http://docs.php.net/manual/en/session.configuration.php#ini.session.save-handler) or the `session_save_handler` function (http://docs.php.net/manual/en/function.session-set-save-handler.php).
Gumbo
+1 for: You should first analyze what the actual bottleneck is.
smack0007